zoukankan      html  css  js  c++  java
  • 保证C# 多线程 UI 响应

    Form1.cs

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Threading; 
     
    namespace WindowsApplication 

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
            } 
     
            class myclass 
            { 
                //单参数构造 
                public myclass(DataGridView dw1) 
                { 
                    dw = dw1; 
                } 
                
                public delegate void mydelegate(); 
                public DataGridView dw = new DataGridView(); 
     
                //Invoke 
                public void C() 
                { 
                    mydelegate mc = new mydelegate(B); 
                    mc.Invoke(); 
     
                } 
     
                //DynamicInvoke 
                public void D() 
                { 
                    mydelegate mc = new mydelegate(B); 
                    mc.DynamicInvoke(); 
     
                } 
                
                //BeginInvoke 
                public void E() 
                { 
                    mydelegate mc = new mydelegate(B); 
                    AsyncCallback asc = new AsyncCallback(F); 
                  //  mc.BeginInvoke(asc,1); 
                    mc.BeginInvoke(nullnull); //不回调 
     
                   
                    
     
                } 
     
                //E的回调方法 
                  public void F( IAsyncResult result) 
                { 
                    MessageBox.Show("方法E完成,此为回调函数"); 
                     
                } 
     
      
               //基本方法 
                public void B() 
                { 
     
                      CheckForIllegalCrossThreadCalls = false;   
                      dw.DataSource = SQLServerDAL.DbHelperSQL.Query("select  top 200000 *  from  费用表").Tables[0].DefaultView; 
                     
                      
                } 
     
                //A方法用线程处理C方法 
                public void A() 
                { 
                    Thread thread = new Thread(C); 
                    thread.IsBackground = true
                    thread.Start(); 
                    
     
                } 
     
            } 
     
     
     
            private void buttonB_Click(object sender, EventArgs e) 
            { 
                myclass mc = new myclass(dataGridView1); 
                Thread thread = new Thread(mc.B); 
                thread.IsBackground = true
                thread.Start(); 
            } 
     
            private void buttonRefresh_Click(object sender, EventArgs e) 
            { 
                //解决方法A的问题,之后滚动条可以滚动了 
                dataGridView1.RightToLeft = RightToLeft.Yes; 
                dataGridView1.RightToLeft = RightToLeft.No; 
     
                //this.Refresh(); 没有作用 
                //dataGridView1.Refresh(); 没有作用 
            } 
     
            private void buttonE_Click(object sender, EventArgs e) 
            { 
                //E方法刚开始不死,一会就假死了 
                myclass mc = new myclass(dataGridView1); 
                Thread thread = new Thread(mc.E); 
                thread.IsBackground = true
                thread.Start(); 
            } 
     
            private void buttonC_Click(object sender, EventArgs e) 
            { 
                myclass mc = new myclass(dataGridView1); 
                Thread thread = new Thread(mc.C); 
                thread.IsBackground = true
                thread.Start(); 
            } 
     
            private void buttonD_Click(object sender, EventArgs e) 
            { 
                myclass mc = new myclass(dataGridView1); 
                Thread thread = new Thread(mc.D); 
                thread.IsBackground = true
                thread.Start(); 
            } 
     
            private void buttonA_Click(object sender, EventArgs e) 
            { 
                myclass mc = new myclass(dataGridView1); 
                mc.A(); 
     
                //或者 
     
                //myclass mc = new myclass(dataGridView1); 
                //Thread thread = new Thread( mc.A); 
                //thread.IsBackground = true; 
                //thread.Start(); 
      
            } 
     
           
        } 

     
    //除了E方法,其他方法UI都始终有响应,但是数据加载完成之后 有共同点问题,那就是滚动条可能没有,或者没有响应, 

    Form1.Designer.cs

    namespace WindowsApplication 

        partial class Form1 
        { 
            /// <summary> 
            /// 必需的设计器变量。 
            /// </summary> 
            private System.ComponentModel.IContainer components = null
     
            /// <summary> 
            /// 清理所有正在使用的资源。 
            /// </summary> 
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 
            protected override void Dispose(bool disposing) 
            { 
                if (disposing && (components != null)) 
                { 
                    components.Dispose(); 
                } 
                base.Dispose(disposing); 
            } 
     
            #region Windows 窗体设计器生成的代码 
     
            /// <summary> 
            /// 设计器支持所需的方法 - 不要 
            /// 使用代码编辑器修改此方法的内容。 
            /// </summary> 
            private void InitializeComponent() 
            { 
                this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
                this.buttonA = new System.Windows.Forms.Button(); 
                this.buttonC = new System.Windows.Forms.Button(); 
                this.buttonD = new System.Windows.Forms.Button(); 
                this.buttonE = new System.Windows.Forms.Button(); 
                this.buttonRefresh = new System.Windows.Forms.Button(); 
                this.buttonB = new System.Windows.Forms.Button(); 
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
                this.SuspendLayout(); 
                //  
                // dataGridView1 
                //  
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
                this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Left; 
                this.dataGridView1.Location = new System.Drawing.Point(00); 
                this.dataGridView1.Name = "dataGridView1"
                this.dataGridView1.RowTemplate.Height = 23
                this.dataGridView1.Size = new System.Drawing.Size(635608); 
                this.dataGridView1.TabIndex = 0
                //  
                // buttonA 
                //  
                this.buttonA.Location = new System.Drawing.Point(686109); 
                this.buttonA.Name = "buttonA"
                this.buttonA.Size = new System.Drawing.Size(12039); 
                this.buttonA.TabIndex = 1
                this.buttonA.Text = "A"
                this.buttonA.UseVisualStyleBackColor = true
                this.buttonA.Click += new System.EventHandler(this.buttonA_Click); 
                //  
                // buttonC 
                //  
                this.buttonC.Location = new System.Drawing.Point(686181); 
                this.buttonC.Name = "buttonC"
                this.buttonC.Size = new System.Drawing.Size(12032); 
                this.buttonC.TabIndex = 2
                this.buttonC.Text = "C"
                this.buttonC.UseVisualStyleBackColor = true
                this.buttonC.Click += new System.EventHandler(this.buttonC_Click); 
                //  
                // buttonD 
                //  
                this.buttonD.Location = new System.Drawing.Point(686237); 
                this.buttonD.Name = "buttonD"
                this.buttonD.Size = new System.Drawing.Size(12036); 
                this.buttonD.TabIndex = 3
                this.buttonD.Text = "D"
                this.buttonD.UseVisualStyleBackColor = true
                this.buttonD.Click += new System.EventHandler(this.buttonD_Click); 
                //  
                // buttonE 
                //  
                this.buttonE.Location = new System.Drawing.Point(686302); 
                this.buttonE.Name = "buttonE"
                this.buttonE.Size = new System.Drawing.Size(12035); 
                this.buttonE.TabIndex = 4
                this.buttonE.Text = "E"
                this.buttonE.UseVisualStyleBackColor = true
                this.buttonE.Click += new System.EventHandler(this.buttonE_Click); 
                //  
                // buttonRefresh 
                //  
                this.buttonRefresh.Location = new System.Drawing.Point(686438); 
                this.buttonRefresh.Name = "buttonRefresh"
                this.buttonRefresh.Size = new System.Drawing.Size(12039); 
                this.buttonRefresh.TabIndex = 6
                this.buttonRefresh.Text = "界面刷新"; 
                this.buttonRefresh.UseVisualStyleBackColor = true
                this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click); 
                //  
                // buttonB 
                //  
                this.buttonB.Location = new System.Drawing.Point(686361); 
                this.buttonB.Name = "buttonB"
                this.buttonB.Size = new System.Drawing.Size(12034); 
                this.buttonB.TabIndex = 7
                this.buttonB.Text = "B"
                this.buttonB.UseVisualStyleBackColor = true
                this.buttonB.Click += new System.EventHandler(this.buttonB_Click); 
                //  
                // Form1 
                //  
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
                this.ClientSize = new System.Drawing.Size(961608); 
                this.Controls.Add(this.buttonB); 
                this.Controls.Add(this.buttonRefresh); 
                this.Controls.Add(this.buttonE); 
                this.Controls.Add(this.buttonD); 
                this.Controls.Add(this.buttonC); 
                this.Controls.Add(this.buttonA); 
                this.Controls.Add(this.dataGridView1); 
                this.Name = "Form1"
                this.Text = "Form1"
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
                this.ResumeLayout(false); 
     
            } 
     
            #endregion 
     
            private System.Windows.Forms.DataGridView dataGridView1; 
            private System.Windows.Forms.Button buttonA; 
            private System.Windows.Forms.Button buttonC; 
            private System.Windows.Forms.Button buttonD; 
            private System.Windows.Forms.Button buttonE; 
            private System.Windows.Forms.Button buttonRefresh; 
            private System.Windows.Forms.Button buttonB; 
        } 

    sql.cs

        using System; 
        using System.Collections; 
        using System.Collections.Specialized; 
        using System.Data; 
        using System.Data.SqlClient; 
        using System.Configuration; 
     
        namespace  SQLServerDAL //可以修改成实际项目的命名空间名称 
        { 
         /**//// <summary> 
         /// Copyright (C) 2010 qanholas  
         /// 数据访问基础类(基于SQLServer) 
         /// 用户可以修改满足自己项目的需要。 
         /// </summary> 
         public abstract class DbHelperSQL 
         { 
          //数据库连接字符串(web.config来配置) 
          //<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />   
             protected static string connectionString = @"Data Source=.\sql2005;Initial Catalog=ycmis;User Id=sa;Password=123456"
          public DbHelperSQL() 
          {    
          } 
        #region 公用方法 
     
          public static int GetMaxID(string FieldName,string TableName) 
          { 
           string strsql = "select max(" + FieldName + ")+1 from " + TableName; 
           object obj = GetSingle(strsql); 
           if (obj == null
           { 
            return 1
           } 
           else 
           { 
            return int.Parse(obj.ToString()); 
           } 
          } 
          public static bool Exists(string strSql, params SqlParameter[] cmdParms) 
          { 
           object obj = GetSingle(strSql, cmdParms); 
           int cmdresult; 
           if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) 
           { 
            cmdresult = 0
           } 
           else 
           { 
            cmdresult = int.Parse(obj.ToString()); 
           } 
           if (cmdresult == 0
           { 
            return false
           } 
           else 
           { 
            return true
           } 
          } 
          #endregion 
     
     
     
          /**//// <summary> 
          /// 执行SQL语句,返回影响的记录数 
          /// </summary> 
          /// <param name="SQLString">SQL语句</param> 
          /// <returns>影响的记录数</returns> 
          public static int ExecuteSql(string SQLString) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           {     
            using (SqlCommand cmd = new SqlCommand(SQLString,connection)) 
            { 
             try 
             {   
              connection.Open(); 
              int rows=cmd.ExecuteNonQuery(); 
              return rows; 
             } 
             catch(System.Data.SqlClient.SqlException E) 
             {      
              connection.Close(); 
              throw new Exception(E.Message); 
             } 
            }     
           } 
          } 
           
          /**//// <summary> 
          /// 执行多条SQL语句,实现数据库事务。 
          /// </summary> 
          /// <param name="SQLStringList">多条SQL语句</param>   
          public static void ExecuteSqlTran(ArrayList SQLStringList) 
          { 
           using (SqlConnection conn = new SqlConnection(connectionString)) 
           { 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand(); 
            cmd.Connection=conn;     
            SqlTransaction tx=conn.BeginTransaction();    
            cmd.Transaction=tx;     
            try 
            {      
             for(int n=0;n<SQLStringList.Count;n++) 
             { 
              string strsql=SQLStringList[n].ToString(); 
              if (strsql.Trim().Length>1
              { 
               cmd.CommandText=strsql; 
               cmd.ExecuteNonQuery(); 
              } 
             }           
             tx.Commit();      
            } 
            catch(System.Data.SqlClient.SqlException E) 
            {   
             tx.Rollback(); 
             throw new Exception(E.Message); 
            } 
           } 
          } 
          /**//// <summary> 
          /// 执行带一个存储过程参数的的SQL语句。 
          /// </summary> 
          /// <param name="SQLString">SQL语句</param> 
          /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> 
          /// <returns>影响的记录数</returns> 
          public static int ExecuteSql(string SQLString,string content) 
          {     
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            SqlCommand cmd = new SqlCommand(SQLString,connection);   
            System.Data.SqlClient.SqlParameter  myParameter = new System.Data.SqlClient.SqlParameter ( "@content", SqlDbType.NText); 
            myParameter.Value = content ; 
            cmd.Parameters.Add(myParameter); 
            try 
            { 
             connection.Open(); 
             int rows=cmd.ExecuteNonQuery(); 
             return rows; 
            } 
            catch(System.Data.SqlClient.SqlException E) 
            {     
             throw new Exception(E.Message); 
            } 
            finally 
            { 
             cmd.Dispose(); 
             connection.Close(); 
            }  
           } 
          }   
          /**//// <summary> 
          /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) 
          /// </summary> 
          /// <param name="strSQL">SQL语句</param> 
          /// <param name="fs">图像字节,数据库的字段类型为image的情况</param> 
          /// <returns>影响的记录数</returns> 
          public static int ExecuteSqlInsertImg(string strSQL,byte[] fs) 
          {   
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            SqlCommand cmd = new SqlCommand(strSQL,connection);  
            System.Data.SqlClient.SqlParameter  myParameter = new System.Data.SqlClient.SqlParameter ( "@fs", SqlDbType.Image); 
            myParameter.Value = fs ; 
            cmd.Parameters.Add(myParameter); 
            try 
            { 
             connection.Open(); 
             int rows=cmd.ExecuteNonQuery(); 
             return rows; 
            } 
            catch(System.Data.SqlClient.SqlException E) 
            {     
             throw new Exception(E.Message); 
            } 
            finally 
            { 
             cmd.Dispose(); 
             connection.Close(); 
            }     
           } 
          } 
           
          /**//// <summary> 
          /// 执行一条计算查询结果语句,返回查询结果(object)。 
          /// </summary> 
          /// <param name="SQLString">计算查询结果语句</param> 
          /// <returns>查询结果(object)</returns> 
          public static object GetSingle(string SQLString) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            using(SqlCommand cmd = new SqlCommand(SQLString,connection)) 
            { 
             try 
             { 
              connection.Open(); 
              object obj = cmd.ExecuteScalar(); 
              if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) 
              {      
               return null
              } 
              else 
              { 
               return obj; 
              }     
             } 
             catch(System.Data.SqlClient.SqlException e) 
             {       
              connection.Close(); 
              throw new Exception(e.Message); 
             }  
            } 
           } 
          } 
          /**//// <summary> 
          /// 执行查询语句,返回SqlDataReader 
          /// </summary> 
          /// <param name="strSQL">查询语句</param> 
          /// <returns>SqlDataReader</returns> 
          public static SqlDataReader ExecuteReader(string strSQL) 
          { 
           SqlConnection connection = new SqlConnection(connectionString);    
           SqlCommand cmd = new SqlCommand(strSQL,connection);     
           try 
           { 
            connection.Open();  
            SqlDataReader myReader = cmd.ExecuteReader(); 
            return myReader; 
           } 
           catch(System.Data.SqlClient.SqlException e) 
           {         
            throw new Exception(e.Message); 
           }    
            
          }   
          /**//// <summary> 
          /// 执行查询语句,返回DataSet 
          /// </summary> 
          /// <param name="SQLString">查询语句</param> 
          /// <returns>DataSet</returns> 
          public static DataSet Query(string SQLString) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            DataSet ds = new DataSet(); 
            try 
            { 
             connection.Open(); 
             SqlDataAdapter command = new SqlDataAdapter(SQLString,connection);     
             command.Fill(ds,"ds"); 
            } 
            catch(System.Data.SqlClient.SqlException ex) 
            {     
             throw new Exception(ex.Message); 
            }    
            return ds; 
           }    
          } 
     
     
     
     
          /**//// <summary> 
          /// 执行SQL语句,返回影响的记录数 
          /// </summary> 
          /// <param name="SQLString">SQL语句</param> 
          /// <returns>影响的记录数</returns> 
          public static int ExecuteSql(string SQLString,params SqlParameter[] cmdParms) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           {     
            using (SqlCommand cmd = new SqlCommand()) 
            { 
             try 
             {   
              PrepareCommand(cmd, connection, null,SQLString, cmdParms); 
              int rows=cmd.ExecuteNonQuery(); 
              cmd.Parameters.Clear(); 
              return rows; 
             } 
             catch(System.Data.SqlClient.SqlException E) 
             {     
              throw new Exception(E.Message); 
             } 
            }     
           } 
          } 
           
            
          /**//// <summary> 
          /// 执行多条SQL语句,实现数据库事务。 
          /// </summary> 
          /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> 
          public static void ExecuteSqlTran(Hashtable SQLStringList) 
          {    
           using (SqlConnection conn = new SqlConnection(connectionString)) 
           { 
            conn.Open(); 
            using (SqlTransaction trans = conn.BeginTransaction())  
            { 
             SqlCommand cmd = new SqlCommand(); 
             try  
             { 
              //循环 
              foreach (DictionaryEntry myDE in SQLStringList) 
              {  
               string  cmdText=myDE.Key.ToString(); 
               SqlParameter[] cmdParms=(SqlParameter[])myDE.Value; 
               PrepareCommand(cmd,conn,trans,cmdText, cmdParms); 
               int val = cmd.ExecuteNonQuery(); 
               cmd.Parameters.Clear(); 
     
               trans.Commit(); 
              }      
             } 
             catch  
             { 
              trans.Rollback(); 
              throw
             } 
            }     
           } 
          } 
          
             
          /**//// <summary> 
          /// 执行一条计算查询结果语句,返回查询结果(object)。 
          /// </summary> 
          /// <param name="SQLString">计算查询结果语句</param> 
          /// <returns>查询结果(object)</returns> 
          public static object GetSingle(string SQLString,params SqlParameter[] cmdParms) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            using (SqlCommand cmd = new SqlCommand()) 
            { 
             try 
             { 
              PrepareCommand(cmd, connection, null,SQLString, cmdParms); 
              object obj = cmd.ExecuteScalar(); 
              cmd.Parameters.Clear(); 
              if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) 
              {      
               return null
              } 
              else 
              { 
               return obj; 
              }     
             } 
             catch(System.Data.SqlClient.SqlException e) 
             {     
              throw new Exception(e.Message); 
             }      
            } 
           } 
          } 
           
          /**//// <summary> 
          /// 执行查询语句,返回SqlDataReader 
          /// </summary> 
          /// <param name="strSQL">查询语句</param> 
          /// <returns>SqlDataReader</returns> 
          public static SqlDataReader ExecuteReader(string SQLString,params SqlParameter[] cmdParms) 
          {   
           SqlConnection connection = new SqlConnection(connectionString); 
           SqlCommand cmd = new SqlCommand();     
           try 
           { 
            PrepareCommand(cmd, connection, null,SQLString, cmdParms); 
            SqlDataReader myReader = cmd.ExecuteReader(); 
            cmd.Parameters.Clear(); 
            return myReader; 
           } 
           catch(System.Data.SqlClient.SqlException e) 
           {         
            throw new Exception(e.Message); 
           }      
            
          }   
           
          /**//// <summary> 
          /// 执行查询语句,返回DataSet 
          /// </summary> 
          /// <param name="SQLString">查询语句</param> 
          /// <returns>DataSet</returns> 
          public static DataSet Query(string SQLString,params SqlParameter[] cmdParms) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            SqlCommand cmd = new SqlCommand(); 
            PrepareCommand(cmd, connection, null,SQLString, cmdParms); 
            using( SqlDataAdapter da = new SqlDataAdapter(cmd) ) 
            { 
             DataSet ds = new DataSet();  
             try 
             {             
              da.Fill(ds,"ds"); 
              cmd.Parameters.Clear(); 
             } 
             catch(System.Data.SqlClient.SqlException ex) 
             {     
              throw new Exception(ex.Message); 
             }    
             return ds; 
            }     
           }    
          } 
     
     
          private static void PrepareCommand(SqlCommand cmd,SqlConnection conn,SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)  
          { 
           if (conn.State != ConnectionState.Open) 
            conn.Open(); 
           cmd.Connection = conn; 
           cmd.CommandText = cmdText; 
           if (trans != null
            cmd.Transaction = trans; 
           cmd.CommandType = CommandType.Text;//cmdType; 
           if (cmdParms != null)  
           { 
            foreach (SqlParameter parm in cmdParms) 
             cmd.Parameters.Add(parm); 
           } 
          } 
     
     
     
          /**//// <summary> 
          /// 执行存储过程 
          /// </summary> 
          /// <param name="storedProcName">存储过程名</param> 
          /// <param name="parameters">存储过程参数</param> 
          /// <returns>SqlDataReader</returns> 
          public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters ) 
          { 
           SqlConnection connection = new SqlConnection(connectionString); 
           SqlDataReader returnReader; 
           connection.Open(); 
           SqlCommand command = BuildQueryCommand( connection,storedProcName, parameters ); 
           command.CommandType = CommandType.StoredProcedure; 
           returnReader = command.ExecuteReader();     
           return returnReader;    
          } 
           
           
          /**//// <summary> 
          /// 执行存储过程 
          /// </summary> 
          /// <param name="storedProcName">存储过程名</param> 
          /// <param name="parameters">存储过程参数</param> 
          /// <param name="tableName">DataSet结果中的表名</param> 
          /// <returns>DataSet</returns> 
          public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName ) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            DataSet dataSet = new DataSet(); 
            connection.Open(); 
            SqlDataAdapter sqlDA = new SqlDataAdapter(); 
            sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters ); 
            sqlDA.Fill( dataSet, tableName ); 
            connection.Close(); 
            return dataSet; 
           } 
          } 
     
           
          /**//// <summary> 
          /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值) 
          /// </summary> 
          /// <param name="connection">数据库连接</param> 
          /// <param name="storedProcName">存储过程名</param> 
          /// <param name="parameters">存储过程参数</param> 
          /// <returns>SqlCommand</returns> 
          private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters) 
          {    
           SqlCommand command = new SqlCommand( storedProcName, connection ); 
           command.CommandType = CommandType.StoredProcedure; 
           foreach (SqlParameter parameter in parameters) 
           { 
            command.Parameters.Add( parameter ); 
           } 
           return command;    
          } 
           
          /**//// <summary> 
          /// 执行存储过程,返回影响的行数   
          /// </summary> 
          /// <param name="storedProcName">存储过程名</param> 
          /// <param name="parameters">存储过程参数</param> 
          /// <param name="rowsAffected">影响的行数</param> 
          /// <returns></returns> 
          public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected ) 
          { 
           using (SqlConnection connection = new SqlConnection(connectionString)) 
           { 
            int result; 
            connection.Open(); 
            SqlCommand command = BuildIntCommand(connection,storedProcName, parameters ); 
            rowsAffected = command.ExecuteNonQuery(); 
            result = (int)command.Parameters["ReturnValue"].Value; 
            //Connection.Close(); 
            return result; 
           } 
          } 
           
          /**//// <summary> 
          /// 创建 SqlCommand 对象实例(用来返回一个整数值)  
          /// </summary> 
          /// <param name="storedProcName">存储过程名</param> 
          /// <param name="parameters">存储过程参数</param> 
          /// <returns>SqlCommand 对象实例</returns> 
          private static SqlCommand BuildIntCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters) 
          { 
           SqlCommand command = BuildQueryCommand(connection,storedProcName, parameters ); 
           command.Parameters.Add( new SqlParameter ( "ReturnValue"
            SqlDbType.Int,4,ParameterDirection.ReturnValue, 
            false,0,0,string.Empty,DataRowVersion.Default,null )); 
           return command; 
          } 
     
         } 
        } 
  • 相关阅读:
    WMS、WCS、PLC、AGV
    SAP消息号修改汇总
    SQL 计算累计和 sum() over( partition by order by )
    DDLS报错数据类型冲突:data type conflict in a selection 解决办法
    SAP销售订单需求类型的确定优先级
    SAP替代,出口U904在RGGBS000中未生成
    订单BOM与销售BOM的区别
    在配置和销售凭证 GET_CONFIG_MODE 间通信时内部出错
    ABAP Write
    php的api接口
  • 原文地址:https://www.cnblogs.com/qanholas/p/1913084.html
Copyright © 2011-2022 走看看