jjzjj

sqlcommand

全部标签

c# - 如何使用 SqlCommand 返回多个结果集?

我可以执行多个查询并仅执行一次SqlCommand返回它们的结果吗? 最佳答案 参见SqlDataReader.NextResult(调用SqlCommand.ExecuteReader返回一个SqlDataReader):Advancesthedatareadertothenextresult[set],whenreadingtheresultsofbatchTransact-SQLstatements.例子:stringcommandText=@"SELECTId,ContactIdFROMdbo.Subscriptions;S

c# - 如何使用 SqlCommand 返回多个结果集?

我可以执行多个查询并仅执行一次SqlCommand返回它们的结果吗? 最佳答案 参见SqlDataReader.NextResult(调用SqlCommand.ExecuteReader返回一个SqlDataReader):Advancesthedatareadertothenextresult[set],whenreadingtheresultsofbatchTransact-SQLstatements.例子:stringcommandText=@"SELECTId,ContactIdFROMdbo.Subscriptions;S

c# - ExecuteNonQuery 要求命令在我的代码中出现事务错误

我在cmd.ExecuteNonQuery上收到以下错误。"ExecuteNonQueryrequiresthecommandtohaveatransactionwhentheconnectionassignedtothecommandisinapendinglocaltransaction.TheTransactionpropertyofthecommandhasnotbeeninitialized."这是我的代码://if(hdRefresh.Value.Length>done.Value.Length||done.Value=="1")//{////WriteYourAddCus

c# - ExecuteNonQuery 要求命令在我的代码中出现事务错误

我在cmd.ExecuteNonQuery上收到以下错误。"ExecuteNonQueryrequiresthecommandtohaveatransactionwhentheconnectionassignedtothecommandisinapendinglocaltransaction.TheTransactionpropertyofthecommandhasnotbeeninitialized."这是我的代码://if(hdRefresh.Value.Length>done.Value.Length||done.Value=="1")//{////WriteYourAddCus

c# - TransactionScope 提前完成

我有一个在TransactionScope中运行的代码块,在这个代码块中我对数据库进行了多次调用。选择、更新、创建和删除整个范围。当我执行删除时,我使用SqlCommand的扩展方法执行它,如果它死锁,它将自动重新提交查询,因为此查询可能会遇到死锁。我相信当遇到死锁并且函数尝试重新提交查询时会出现问题。这是我收到的错误:Thetransactionassociatedwiththecurrentconnectionhascompletedbuthasnotbeendisposed.Thetransactionmustbedisposedbeforetheconnectioncanbeu

c# - TransactionScope 提前完成

我有一个在TransactionScope中运行的代码块,在这个代码块中我对数据库进行了多次调用。选择、更新、创建和删除整个范围。当我执行删除时,我使用SqlCommand的扩展方法执行它,如果它死锁,它将自动重新提交查询,因为此查询可能会遇到死锁。我相信当遇到死锁并且函数尝试重新提交查询时会出现问题。这是我收到的错误:Thetransactionassociatedwiththecurrentconnectionhascompletedbuthasnotbeendisposed.Thetransactionmustbedisposedbeforetheconnectioncanbeu

c# - 从 SqlCommand 对象获取生成的 SQL 语句?

我有以下代码:UsingcmdAsSqlCommand=Connection.CreateCommandcmd.CommandText="UPDATEsomeTableSETValue=@Value"cmd.CommandText&="WHEREId=@Id"cmd.Parameters.AddWithValue("@Id",1234)cmd.Parameters.AddWithValue("@Value","myValue")cmd.ExecuteNonQueryEndUsing我想知道是否有任何方法可以将最终的SQL语句作为字符串获取,它应该如下所示:UPDATEsomeTable

c# - 从 SqlCommand 对象获取生成的 SQL 语句?

我有以下代码:UsingcmdAsSqlCommand=Connection.CreateCommandcmd.CommandText="UPDATEsomeTableSETValue=@Value"cmd.CommandText&="WHEREId=@Id"cmd.Parameters.AddWithValue("@Id",1234)cmd.Parameters.AddWithValue("@Value","myValue")cmd.ExecuteNonQueryEndUsing我想知道是否有任何方法可以将最终的SQL语句作为字符串获取,它应该如下所示:UPDATEsomeTable

c# - 在 SqlCommand 中传递数组参数

我正在尝试将数组参数传递给C#中的SQL命令,如下所示,但它不起作用。有人见过吗?stringsqlCommand="SELECT*fromTableAWHEREAgeIN(@Age)";SqlConnectionsqlCon=newSqlConnection(connectString);SqlCommandsqlComm=newSqlCommand();sqlComm.Connection=sqlCon;sqlComm.CommandType=System.Data.CommandType.Text;sqlComm.CommandText=sqlCommand;sqlComm.Co

c# - 关闭 SqlConnection 和 SqlCommand c#

在我的DAL中,我这样写查询:using(SQLConnectionconn="connectionstringhere"){SQLCommandcmd=new("sqlquery",conn);//executeitblahblah}现在我突然想到我没有明确关闭SQLCommand对象。现在我知道“using”block会处理SQLConnection对象,但这是否也会处理SQLCommand对象?如果不是,那我就有严重的问题了。我将不得不在成千上万行代码的SQLCommand上输入“使用”,或者在数百种方法上执行cmd.Close()。请告诉我,如果放入using或closing命