2012-12-03 103 views
25

我正在创建一个项目,我需要在单个sql连接中运行2-3个sql命令。 这里是我写的代码:如何在单个SQL连接中运行多个SQL命令?

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\project.mdf;Integrated Security=True"); 
con.Open(); 
SqlCommand cmd = new SqlCommand("select * from " + mytags.Text + " ", con); 
SqlDataReader rd = cmd.ExecuteReader(); 
if (rd.Read()) 
{ 
    con.Close(); 
    con.Open(); 
    SqlCommand cmd1 = new SqlCommand("insert into " + mytags.Text + " values ('[email protected]','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','"+mytags.Text+"')", con); 
    cmd1.ExecuteNonQuery(); 
    label.Visible = true; 
    label.Text = "Date read and inserted"; 
} 
else 
{ 
    con.Close(); 
    con.Open(); 
    SqlCommand cmd2 = new SqlCommand("create table " + mytags.Text + " (session VARCHAR(MAX) , Price int , Description VARCHAR(MAX), Date VARCHAR(20),tag VARCHAR(10))", con); 
    cmd2.ExecuteNonQuery(); 
    con.Close(); 
    con.Open(); 
    SqlCommand cmd3 = new SqlCommand("insert into " + mytags.Text + " values ('" + Session + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + mytags.Text + "')", con); 
    cmd3.ExecuteNonQuery(); 
    label.Visible = true; 
    label.Text = "tabel created"; 
    con.Close(); 
} 

我试图删除错误,我得到了该连接不会else条件。请查看代码并建议是否有任何错误或其他解决方案。

+0

如果你已经有一个表的名称相同,但在它没有数据会发生什么范围内运行多个SQL命令(更新2列)?还有,你是否考虑过如果有人在你插入的文本框中输入SQL,会发生什么? – Greg

回答

35

只需更改SqlCommand.CommandText而不是每次创建新的SqlCommand。无需关闭并重新打开连接。

// Create the first command and execute 
var command = new SqlCommand("<SQL Command>", myConnection); 
var reader = command.ExecuteReader(); 

// Change the SQL Command and execute 
command.CommandText = "<New SQL Command>"; 
command.ExecuteNonQuery(); 
+0

我已更新代码: – user1831272

+0

var cmd = new SqlCommand(“select * from”+ mytags.Text +“”,con); var rd = cmd.ExecuteReader(); if(rd.Read()) { cmd.CommandText =“insert into”+ mytags.Text +“values('[email protected]','”+ TextBox3.Text +“','” + TextBox4.Text +“','”+ TextBox5.Text +“','”+ mytags.Text +“')”; cmd.ExecuteNonQuery(); – user1831272

+0

仍然不起作用 – user1831272

17

以下内容应该有效。保持单个连接一直打开,只需创建新命令并执行它们。

using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    connection.Open(); 
    using (SqlCommand command1 = new SqlCommand(commandText1, connection)) 
    { 
    } 
    using (SqlCommand command2 = new SqlCommand(commandText2, connection)) 
    { 
    } 
    // etc 
} 
+1

我的第一个命令是创建一个临时表。我的下一个命令需要使用该临时表。在你的例子中,临时表消失。当我做了一个跟踪,跟踪显示我“exec CreateTableSproc”。我怎样才能让临时表被下一个命令看到? –

7

这很可能是通过SQL注入攻击的方式。这是值得的阅读,并相应地调整您的查询。

也许看看甚至为此创建一个存储过程,并使用类似sp_executesql的东西,当动态SQL是一个需求(即未知的表名等)时可以提供一些保护。欲了解更多信息,请查看this link

7

我还没有测试过,但主要想法是:在每个查询上放置分号。

SqlConnection connection = new SqlConnection(); 
SqlCommand command = new SqlCommand(); 
connection.ConnectionString = connectionString; // put your connection string 
command.CommandText = @" 
    update table 
    set somecol = somevalue; 
    insert into someTable values(1,'test');"; 
command.CommandType = CommandType.Text; 
command.Connection = connection; 

try 
{ 
    connection.Open(); 
} 
finally 
{ 
    command.Dispose(); 
    connection.Dispose(); 
} 

更新: 你可以按照 Is it possible to have multiple SQL instructions in a ADO.NET Command.CommandText property?

+0

它的工作原理...干杯! –

11

刚刚启用此属性在连接字符串中:

sqb.MultipleActiveResultSets = true;

这propperty alows多的DataReader

一个打开的连接
+1

这我相信应该是答案。 – GibralterTop

+1

如果您打算启用MARS,请务必阅读以下“特殊注意事项”:https://msdn.microsoft。com/en-us/library/h32h3abf(v = vs.110).aspx –

+0

我找到了我的答案,并在 –

0

这里你可以找到Postgre例如,此代码单独的SQL连接

public static class SQLTest 
    { 
     public static void NpgsqlCommand() 
     { 
      using (NpgsqlConnection connection = new NpgsqlConnection("Server = ; Port = ; User Id = ; " + "Password = ; Database = ;")) 
      { 
       NpgsqlCommand command1 = new NpgsqlCommand("update xy set xw = 'a' WHERE aa='bb'", connection); 
       NpgsqlCommand command2 = new NpgsqlCommand("update xy set xw = 'b' where bb = 'cc'", connection); 
       command1.Connection.Open(); 
       command1.ExecuteNonQuery(); 
       command2.ExecuteNonQuery(); 
       command2.Connection.Close(); 
      } 
     } 
    } 
+0

以上回答。请编辑以解释此代码的成就。 –