我使用的是sql server 2008。我在sql server中有数据库,其中包含表和视图。目前,我正在c#开发一个程序,可以从sql server中的表中选择视图,并在mysql中导出为表。到目前为止,我已经在c中制作了一个表单,它允许我选择一个正在运行的sql server,然后选择它的可用数据库,然后根据选定的数据库选择表和视图。对于sql部分,我已经完成了,但是我不知道如何将视图作为表从sql导出到mysql。如何在c中给出mysql的连接字符串,以便它创建一个从mysql中选择的视图表?。以下是我目前的代码:
private void button1_Click(object sender, EventArgs e)
{
List<String> ServerNames = new List<String>();
List<String> mysqlname = new List<String>();
System.Data.Sql.SqlDataSourceEnumerator servers = System.Data.Sql.SqlDataSourceEnumerator.Instance;
DataTable serversTable = servers.GetDataSources();
foreach (DataRow row in serversTable.Rows)
{
string serverName = row[0].ToString();
try
{
if (row[1].ToString() != "")
{
serverName += "\\" + row[1].ToString();
}
}
catch
{
}
ServerNames.Add(serverName);
serverscombo.Items.Add(serverName);
}
}
private void serverscombo_SelectedIndexChanged(object sender, EventArgs e)
{
string fromcombo = serverscombo.SelectedItem.ToString();
servertxt.Text = fromcombo;
}
private void servertxt_TextChanged(object sender, EventArgs e)
{
List<String> databases = new List<String>();
System.Data.SqlClient.SqlConnectionStringBuilder connection = new System.Data.SqlClient.SqlConnectionStringBuilder();
connection.DataSource = servertxt.Text;
// enter credentials if you want
//connection.UserID = //get username;
// connection.Password = //get password;
connection.IntegratedSecurity = true;
String strConn = connection.ToString();
connglob = strConn;
//create connection
SqlConnection sqlConn = new SqlConnection(strConn);
//open connection
sqlConn.Open();
//get databases
DataTable tblDatabases = sqlConn.GetSchema("Databases");
//close connection
sqlConn.Close();
//add to list
foreach (DataRow row in tblDatabases.Rows)
{
String strDatabaseName = row["database_name"].ToString();
databases.Add(strDatabaseName);
databasenamescombo.Items.Add(strDatabaseName);
}
}
private void databasenamescombo_SelectedIndexChanged(object sender, EventArgs e)
{
string fromcombo2 = databasenamescombo.SelectedItem.ToString();
//txtblob = fromcombo2;
databasetxt.Text = fromcombo2;
string val = databasetxt.Text;
txtblob = val;
}
private void databasetxt_TextChanged(object sender, EventArgs e)
{
List<String> tble = new List<string>();
using (SqlConnection con = new SqlConnection(connglob))
{
con.Open();
System.Text.StringBuilder myQuery = new System.Text.StringBuilder();
//myQuery.AppendFormat("SELECT TABLE_NAME FROM {0}.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'", databasetxt.Text);
using (SqlCommand com = new SqlCommand("SELECT TABLE_NAME FROM [" + databasetxt.Text + "].INFORMATION_SCHEMA.TABLES",con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
//comboBox1.Items.Clear();
while (reader.Read())
{
comboBox1.Items.Add((string)reader["TABLE_NAME"]);
}
}
}
colortxt.BackColor = Color.LightGreen;
}
暂无答案!
目前还没有任何答案,快来回答吧!