use*_*272 38 c# sql-server ado.net sqlcommand sqlconnection
我正在创建一个项目,我需要在单个sql连接中运行2-3个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 ('fname.lname@gmail.com','" + 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();
}
Run Code Online (Sandbox Code Playgroud)
我试图删除错误,我得到了连接不会其他条件.请查看代码并建议是否有任何错误或任何其他解决方案.
lum*_*ck4 44
只需改变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();
Run Code Online (Sandbox Code Playgroud)
aba*_*hev 29
以下应该有效.保持单个连接始终打开,只需创建新命令并执行它们.
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command1 = new SqlCommand(commandText1, connection))
{
}
using (SqlCommand command2 = new SqlCommand(commandText2, connection))
{
}
// etc
}
Run Code Online (Sandbox Code Playgroud)
Ham*_*ago 19
只需在连接字符串中启用此属性:
sqb.MultipleActiveResultSets = true;
这个属性为多个数据加载器提供了一个开放式连接
Muj*_*key 10
我没有测试,但主要的想法是:在每个查询上加分号.
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();
}
Run Code Online (Sandbox Code Playgroud)
更新: 您可以关注 是否可以在ADO.NET Command.CommandText属性中包含多个SQL指令?太
顺便说一下,这可能会通过SQL注入进行攻击.在阅读并相应调整您的查询时,这是值得的.
也许看看甚至为此创建存储过程并使用类似sp_executesql的东西,当动态sql是一个要求时(即未知的表名等)可以提供一些保护.有关详细信息,请查看此链接.
没有人提到过这一点,但您也可以使用 ; 分隔您的命令。同一个 CommandText 中的分号:
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = @"update table ... where myparam=@myparam1 ; " +
"update table ... where myparam=@myparam2 ";
comm.Parameters.AddWithValue("@myparam1", myparam1);
comm.Parameters.AddWithValue("@myparam2", myparam2);
conn.Open();
comm.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果有人感兴趣的话,可以使用多个非查询示例。
using (OdbcConnection DbConnection = new OdbcConnection("ConnectionString"))
{
DbConnection.Open();
using (OdbcCommand DbCommand = DbConnection.CreateCommand())
{
DbCommand.CommandText = "INSERT...";
DbCommand.Parameters.Add("@Name", OdbcType.Text, 20).Value = "name";
DbCommand.ExecuteNonQuery();
DbCommand.Parameters.Clear();
DbCommand.Parameters.Add("@Name", OdbcType.Text, 20).Value = "name2";
DbCommand.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
160479 次 |
| 最近记录: |