【发布时间】:2016-10-25 10:15:26
【问题描述】:
这是我拥有的代码,在组合框中选择一个连接字符串时效果很好
如何在多个连接中执行相同的 SQL 查询,而不是一键单击??
public string connstring = "";
private void cmbSrv_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbSrv.SelectedIndex == 0)
{
connstring = @"Data Source=tcp:10.1.0.100;Initial Catalog=Database1;User ID=user;Password=pass;MultipleActiveResultSets=true;";
}
else if (cmbSrv.SelectedIndex == 1)
{
connstring = @"Data Source=tcp:10.0.0.100;Initial Catalog=Database2 ;User ID=user;Password=pass;MultipleActiveResultSets=true;";
}
}
private void btnKonfirm_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(connstring))
{
SqlCommand cmd1 = new SqlCommand();
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
SqlCommand command1 =
new SqlCommand("DELETE FROM TABLE1 WHERE ID="+textbox1+"", connection);
command1.ExecuteNonQuery();
connection.Close();
}
}
【问题讨论】:
-
检查我的答案,如果有什么不清楚的地方告诉我。
标签: c# sql connection