PetShop DBUtility 类库的学习

ExecuteNonQuery 方法

<方法一>

<Summary>
Execute a SQLCommand (that returns no resultset) against(依照) the database specified in the connection string using the provided parameters.
根据指定的数据库连接参数连接数据库后执行命令
</Summary)

eg.
int result=ExecuteNonQuery(connString,CommandType.StoredProcedure,”PublishOrders”,new SqlParameter(“@prodid”,24));

Paramter:

connectionString-----a valid connection string for a SqlConnection
commandType-----the command type (stored procedure,text,etc.)
commandText-----the stored procedure name or T-SQL command
commandParameters-----an array of SqlParameters used to execute the command

Returns

an int representing the number of rows affected by the command

-------------------------------------------------------------------------------------------------------------------------------------------------

public static int ExecuteNonQuery(string connectionString,CommandType cmdType,string cmdText,params SqlParameter[] commandParameters)

{

      SqlCommand cmd=new SqlCommand();

      using(SqlConnection conn=new SqlConnection(connectionString)){                            //连接用using 语句

            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
在已有连接上执行一条SQL语句命令(不返回结果集)
</Summary>

-------------------------------------------------------------------------------------------------------------------------------------------------

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>

Execute a SqlCommand (That returns no resultset) using an existing SQL Transaction
在已有的事务中执行一句不返回结果集的语句
</Summary>

-------------------------------------------------------------------------------------------------------------------------------------------------

public static int ExecuteNonQuery(SqlTransaction trans,CommandType cmdType,string cmdText,params SqlParameter[] commandParameters)

{

      SqlCommand cmd=new SqlCommand();

            PrepareCommand(cmd,trans,null,cmdType,cmdText,commandParameters);

            int val=cmd.ExecuteNonQuery();

            cmd.Parameters.Clear();

            return val;

     

}

ExecuteReader方法

/// <summary>

/// Execute a SqlCommand that returns a resultset against the database specified in the connection string

/// using the provided parameters.

/// </summary>

/// <remarks>

/// e.g.:

/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));

/// </remarks>

/// <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>A SqlDataReader containing the results</returns>

-------------------------------------------------------------------------------------------------------------------------------------------------

public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {

           Command cmd = new SqlCommand();

           Connection conn = new SqlConnection(connectionString);

            // we use a try/catch here because if the method throws an exception we want to

            // close the connection throw code, because no datareader will exist, hence the

            // commandBehaviour.CloseConnection will not work

             try {

                       PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);

                       SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                       cmd.Parameters.Clear();

                       return rdr;

                   }

             catch {

                          conn.Close();

                          throw;

                      }

}

PrepareCommand方法

格式化命令

/// <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);

                   }

              }

posted @ 2009-04-22 11:57  斑点海豚---寂静的港湾  阅读(375)  评论(0编辑  收藏  举报