当前位置:编程学习 > C#/ASP.NET >>

提示出错:事务不是与当前连接无关联,就是已完成,高手帮忙啊

SqlTransaction st;

            try
            {
                using (SqlConnection conn = new SqlConnection(SqlHelper.connectionString1))
                {
                    conn.Open();
                    st = conn.BeginTransaction();
                    //st = st.BeginTransaction();
                    try
                    {
                        for (int ii = 0; ii < _Dt.Rows.Count; ii++)
                        {
                            string sql_Insert = "insert into ck_ware_rfid_rk (rfid,maker,sj,zc,ysqy,barcode,fz) values (@rfid,@maker,@sj,@zc,@ysqy,@barcode,@fz)";


                            SqlParameter[] para = new SqlParameter[]
                                        {
                                        new SqlParameter("@rfid", _Dt.Rows[ii]["rfid"].ToString() ),
                                         new SqlParameter("@maker", _Dt.Rows[ii]["maker"].ToString() ),
                                          new SqlParameter("@sj",Convert.ToDateTime(_Dt.Rows[ii]["sj"].ToString())  ),
                                           new SqlParameter("@zc", _Dt.Rows[ii]["zc"].ToString() ),
                                            new SqlParameter("@ysqy", _Dt.Rows[ii]["ysqy"].ToString() ),
                                             new SqlParameter("@barcode", _Dt.Rows[ii]["barcode"].ToString() ),
                                              new SqlParameter("@fz",_Dt.Rows[ii]["fz"].ToString() )

                                        
                                        };
                            int i_row = SqlHelper.ExecuteCommand(st, CommandType.Text, sql_Insert, true,para);
                            icount++;
                        }
                        st.Commit();
                    }
                    catch (Exception)
                    {
                        st.Rollback();
                        MessageBox.Show("保存失败");
                        throw;
                    }
                    //st.Commit();
                    if (icount == _Dt.Rows.Count)
                    {
                        for (int i = 0; i < lvTagList.Items.Count; i++)
                        {
                            lvTagList.Items[i].SubItems[4].Text = "y";
                        }
                        this._Dt.Clear();
                        MessageBox.Show("保存成功");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
          }



public static int ExecuteCommand(string ConnString, CommandType commandType, string proc_name, bool _Type, SqlParameter[] values)
        {
            SqlConnection Connection = null;
            if (_Type)
            {
                Connection = new SqlConnection(connectionString1);

            }
            else
            {
                Connection = new SqlConnection(connectionString2);

            }
            
            try
            {
                Connection.Open();
                SqlCommand cmd = new SqlCommand(proc_name, Connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddRange(values);
                int count = cmd.ExecuteNonQuery();
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.Close();
            }
        } --------------------编程问答-------------------- 你开头定义的SqlConnection,同SqlHelper.ExecuteCommand方法内的SqlConnection,分明是两个不同的连接对象。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,