c# - C#中的db上下文事务

c# - C#中的db上下文事务,第1张

我正在尝试从c#context提交事务:

private TestDbEntities context =new TestDbEntities(ConnectionString); 

this.context.Connection.Open();
System.Data.Common.DbTransaction transaction = this.context.Connection.BeginTransaction();
DbCommand command = this.context.Connection.CreateCommand();
command.CommandText = "Some Insert/Update Query";
command.ExecuteNonQuery();
transaction.Commit();

但我收到错误消息:

  

"查询语法无效。接近标识符Table_Name,第1行,   第8栏。"

当我直接在sql management studio上直接执行时,查询似乎很好。 有什么解决方案吗?

最佳答案:

1 个答案:

答案 0 :(得分:-2)

 SqlConnection conn = new SqlConnection(); 
 SqlCommand command1 = new SqlCommand(); 
 SqlCommand command2 = new SqlCommand(); 

            SqlTransaction trans = conn.BeginTransaction();
            command1.Transaction = trans;
            command2.Transaction = trans;

            try
            {
                command1.ExecuteNonQuery();
                command2.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
            }
            finally
            {

            }
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复