我想传入一个语句去执行多个操作,我不知道传入SQL的格式怎么写更合理些。
例如:
string strSQL="update a set a.total=b.total from tablea a, tableb b where a.id=b.id delete from tableb"
SqlHelper.ExecuteNonQuery(DataConn,CommandType.Text,strSQL);上面的strSQL实际上是由"update a set a.total=b.total from tablea a, tableb b where a.id=b.id"和"delete from tableb"组成,
请问可以直接象上面一样写在一起吗?有什么更合理的写法吗?

解决方案 »

  1.   

    我觉得你这个用存储过程来作最好
    先update然后delete,而且应该用到事务
      

  2.   

    /// <summary>
            /// 更新公司简介
            /// </summary>
            /// <param name="str_ContentType">内容类别</param>
            /// <param name="str_Content">公司简介内容</param>
            /// <returns>True :更新成功 False 更新失败</returns>
            public bool b_updateCompanyIntroduce(string str_ContentType,string str_Content)
            {
                //首先先删除,指定的记录
                System.Data.SqlClient.SqlConnection sql_conn = new SqlConnection(SqlHelper.ConnectionString);
                 
                sql_conn.Open();            
                SqlTransaction trans = sql_conn.BeginTransaction();            try
                {                string strSQL = "delete from CN_Content where ContentType=@ContentType";                SqlParameter[] param_1 = new SqlParameter[1];
                    param_1[0] = new SqlParameter("@ContentType", SqlDbType.NVarChar, 50);
                    param_1[0].Value = str_ContentType;                Common.SqlHelper.ExecuteNonQuery(trans, CommandType.Text, strSQL, param_1);
                    //然后新增                SqlParameter[] param = new SqlParameter[3];
                    param[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);                string str_SelectContentType = "";
                    switch (str_ContentType)
                    {
                        case "1":
                            str_SelectContentType = "公司简介";
                            break;
                        case "2":
                            str_SelectContentType = "公司景观";
                            break;
                        case "3":
                            str_SelectContentType = "生产实力";
                            break;
                        case "4":
                            str_SelectContentType = "品质控制";
                            break;
                        case "5":
                            str_SelectContentType = "工艺技术";
                            break;
                        case "20":
                            str_SelectContentType = "广聘英才";
                            break;                }                param[0].Value = str_SelectContentType;                param[1] = new SqlParameter("@Content", SqlDbType.NVarChar, 20000);
                    param[1].Value = str_Content;                param[2] = new SqlParameter("@ContentType", SqlDbType.NVarChar, 50);
                    param[2].Value = str_ContentType;
                    string strSQL_insert = "insert into CN_Content(Title,Content,ContentType) values (@Title,@Content,@ContentType)";
                    Common.SqlHelper.ExecuteNonQuery(trans, CommandType.Text, strSQL_insert, param);                trans.Commit();
     
                }
                catch
                {
                    trans.Rollback();
                    sql_conn.Close();
                    return false;
                    
                }         
                sql_conn.Close();
                return true;
                   }
      

  3.   

    再弱弱的问下,将我上面的
    string strSQL="update a set a.total=b.total from tablea a, tableb b where a.id=b.id delete from tableb"
    写成存储过程如下:
    CREATE PROCEDURE SP_Mytest
    AS
    update a set a.total=b.total from tablea a, tableb b where a.id=b.id 
    delete from tableb
    GO
    但要用到事任务,这个存储过程该怎么写呢?
      

  4.   

    对于多个才操作的语句 要用 Transaction
      来保持数据同步
      

  5.   

    CREATE PROCEDURE SP_Mytest
    AS
    begin
    begin tran
    update a set a.total=b.total from tablea a, tableb b where a.id=b.id 
    if @@error<>0
    begin
    --操作失败,则事务回滚
    rollback tran
    end
    delete from tableb
    if @@error<>0
    begin
    --操作失败,则事务回滚
    rollback tran
    end
    --如果操作执行正确,则提交事务
    commit tran
    end
    GO各位大侠!写成上面那样,是否正确呢?
      

  6.   


    CREATE PROCEDURE SP_Mytest
    AS
    --开始事务
    set xact_abort on
    begin tran tran1update a set a.total=b.total from tablea a, tableb b where a.id=b.id delete from tableb
    --执行事务
    IF @@error>0
    rollback tran tran1
    ELSE
    commit tran  tran1
    GO
      

  7.   

    既可以用存储过程去写也可以用  jhtchina(我的目标是做Microsoft MVP讲师的写法,但必须要用事物
    存储过程中用事物:begin tran ........