Live Writer的代码插件测试

Saturday, March 17, 2007

   1: private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
   2: {
   3:     if (connection == null) throw new ArgumentNullException("connection");
   4:  
   5:     bool mustCloseConnection = false;
   6:     // Create a command and prepare it for execution
   7:     SqlCommand cmd = new SqlCommand();
   8:     try
   9:     {
  10:         PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
  11:  
  12:         // Create a reader
  13:         SqlDataReader dataReader;
  14:  
  15:         // Call ExecuteReader with the appropriate CommandBehavior
  16:         if (connectionOwnership == SqlConnectionOwnership.External)
  17:         {
  18:             dataReader = cmd.ExecuteReader();
  19:         }
  20:         else
  21:         {
  22:             dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  23:         }
  24:  
  25:         // Detach the SqlParameters from the command object, so they can be used again.
  26:         // HACK: There is a problem here, the output parameter values are fletched 
  27:         // when the reader is closed, so if the parameters are detached from the command
  28:         // then the SqlReader can磘 set its values. 
  29:         // When this happen, the parameters can磘 be used again in other command.
  30:         bool canClear = true;
  31:         foreach (SqlParameter commandParameter in cmd.Parameters)
  32:         {
  33:             if (commandParameter.Direction != ParameterDirection.Input)
  34:                 canClear = false;
  35:         }
  36:  
  37:         if (canClear)
  38:         {
  39:             cmd.Parameters.Clear();
  40:         }
  41:  
  42:         return dataReader;
  43:     }
  44:     catch
  45:     {
  46:         if (mustCloseConnection)
  47:             connection.Close();
  48:         throw;
  49:     }
  50: }

还不错,和WLW结合得挺好的,暂时就先用它吧

作者的blog,但是他给的那个插件下载地在我这里直接是登录不上去的,你呢?

0 COMMENTS: