简体   繁体   中英

C# re-using SQLCommand

How do I reuse a SQLCommand for say multiple queries?

eg

SqlCommand mycommand = new SqlCommand("SElect * from BKLAH", myConnection);
mycommand.ExecuteNonQuery();

Now I want to use mycommand again but use a different SQL query. How would I do this?

您可以设置命令的CommandText属性,如下所示:

mycommand.CommandText = @"UPDATE BKLAH SET a = 5 WHERE id=@id";

I would use it in a USING section.

Command 1:

using (SqlCommand mycommand = new SqlCommand("SElect * from BKLAH", myConnection))
{
mycommand.ExecuteNonQuery();
}

Command 2:

using (SqlCommand mycommand = new SqlCommand("SElect * from BKLAH2", myConnection))
{
mycommand.ExecuteNonQuery();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM