sqlDBHelper.ExecuteNonquery() 怎么写?谢谢高手指点下

解决方案 »

  1.   


    /// <summary>
            /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string 
            /// using the provided parameters.
            /// </summary>
            /// <res>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </res>
            /// <param name="connectionString">a valid connection string for a SqlConnection</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {            SqlCommand cmd = new SqlCommand();            using (SqlConnection conn = new SqlConnection(connectionString)) {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                    int val = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return val;
                }
            }        /// <summary>
            /// Execute a SqlCommand (that returns no resultset) against an existing database connection 
            /// using the provided parameters.
            /// </summary>
            /// <res>
            /// e.g.:  
            ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
            /// </res>
            /// <param name="conn">an existing database connection</param>
            /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
            /// <param name="commandText">the stored procedure name or T-SQL command</param>
            /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
            /// <returns>an int representing the number of rows affected by the command</returns>
            public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {            SqlCommand cmd = new SqlCommand();            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
    /// <summary>
            /// Prepare a command for execution
            /// </summary>
            /// <param name="cmd">SqlCommand object</param>
            /// <param name="conn">SqlConnection object</param>
            /// <param name="trans">SqlTransaction object</param>
            /// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
            /// <param name="cmdText">Command text, e.g. Select * from Products</param>
            /// <param name="cmdParms">SqlParameters to use in the command</param>
            private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, 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 = cmdType;            if (cmdParms != null) {
                    foreach (SqlParameter parm in cmdParms)
                        cmd.Parameters.Add(parm);
                }
            }
        }
      

  2.   

            public static SqlConnection DBCon()
            {
                return new SqlConnection("server=.;database=ResearchDB;uid=sa;pwd=123123123");
            }
            public static void Message(string a, string b)
            {
                MessageBox.Show(a, b, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            public static void InsertData(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                if (cmd.ExecuteNonQuery() > 0)
                {
                    Message("提示:添加成功!", "提示");
                }
                else
                {
                    Message("提示:添加 失败!", "警告");
                }
                conn.Close();
            }        public static SqlCommand getCmd(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);            return cmd;
            }
            public static SqlDataReader ExecuteReader(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                //conn.Close();
                return sdr;
            }        public static int ExecuteNoQuery(string strSQL)
            {
                int i = 0;
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                if (cmd.ExecuteNonQuery()!=0)
                    i = 1;
                return i;
            }
      

  3.   

            public static SqlConnection DBCon()
            {
                return new SqlConnection("server=.;database=ResearchDB;uid=sa;pwd=123123123");
            }
            public static void Message(string a, string b)
            {
                MessageBox.Show(a, b, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            public static void InsertData(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                if (cmd.ExecuteNonQuery() > 0)
                {
                    Message("提示:添加成功!", "提示");
                }
                else
                {
                    Message("提示:添加 失败!", "警告");
                }
                conn.Close();
            }        public static SqlCommand getCmd(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);            return cmd;
            }
            public static SqlDataReader ExecuteReader(string strSQL)
            {
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                //conn.Close();
                return sdr;
            }        public static int ExecuteNoQuery(string strSQL)
            {
                int i = 0;
                SqlConnection conn = DBCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                if (cmd.ExecuteNonQuery()!=0)
                    i = 1;
                return i;
            }
      

  4.   

    有完全版的DBHeper?发给我一份吧?