1

我有以下连接字符串,以下两种方法处理不同的存储过程:我觉得这段代码有点冗长,想知道是否有可能减少可能的冗余?

是否有更精简的方法来使用相同的连接字符串调用各个方法。

简而言之; 我只是问是否有办法用更少的代码来做我正在做的事情?

我在一个名为的类中拥有所有这些Database

class Database
{
  public static Int32 intReturnCode { get; set; }
  public static string DatabaseStatus { get; set; }

  static string source = 
  @"Server=MyServer;Database=MyDatabase;Integrated Security=True; Timeout=30";

  public static void InsertTracking(string UserID, string Department)
  {
    try
    {
      using (SqlConnection databaseConnection = new SqlConnection(source))
      {
        using (SqlCommand cmdSQL = new SqlCommand("usp_InsertUser", databaseConnection))

        {
          cmdSQL.CommandType = CommandType.StoredProcedure;

          cmdSQL.Parameters.Add(new SqlParameter("@UserID", SqlDbType.NChar, 8));
          cmdSQL.Parameters["@UserID"].Value = UserID;
          cmdSQL.Parameters["@UserID"].Direction = ParameterDirection.Input;

          cmdSQL.Parameters.Add(new SqlParameter("@Department", SqlDbType.NChar, 4));
          cmdSQL.Parameters["@Department"].Value = Department;
          cmdSQL.Parameters["@Department"].Direction = ParameterDirection.Input;

          cmdSQL.Parameters.Add(new SqlParameter("@ReturnCode", SqlDbType.Int));
          cmdSQL.Parameters["@ReturnCode"].Direction = ParameterDirection.ReturnValue;                  

          databaseConnection.Open();
          cmdSQL.ExecuteNonQuery();
          intReturnCode = Convert.ToInt16(cmdSQL.Parameters["@ReturnCode"].Value);

          databaseConnection.Close();
        }
      }
     }
     catch (Exception ex)
     {
       DatabaseStatus = ex.Message;
     }
   }

  public static int InitializeDatabase()
  {
    try
    {
      using (SqlConnection databaseConnection = new SqlConnection(source))
      {
        using (SqlCommand cmdSQL = new SqlCommand("usp_InitializeDatabase", databaseConnection))

        {
          cmdSQL.CommandType = CommandType.StoredProcedure;
          cmdSQL.Parameters.Add(new SqlParameter("@ReturnCode", SqlDbType.Int));
          cmdSQL.Parameters["@ReturnCode"].Direction = ParameterDirection.ReturnValue; 

          databaseConnection.Open();
          cmdSQL.ExecuteNonQuery();
          intReturnCode = Convert.ToInt16(cmdSQL.Parameters["@ReturnCode"].Value);

         databaseConnection.Close();
       }
     }
   }
   catch (Exception ex)
   {
     DatabaseStatus = ex.Message;
   }

   return intReturnCode;
  }
}
4

1 回答 1

0

您可以共享 SqlCommand(usp_InsertUser SP)和 SqlConnection 瞬间。无需一次又一次地“使用”来创建它们。

 static class Database
 {
     public static Int32 intReturnCode { get; set; }
     public static string DatabaseStatus { get; set; }
     static SqlConnection databaseConnection;
     static SqlCommand sqlCmd_usp_InsertUser;
     static SqlCommand sqlCmd_usp_InitializeDatabase;
     string source = @"Server=MyServer;Database=MyDatabase;Integrated Security=True; Timeout=30";

     //static initalizer
     static Database()
     {
         string source = @"Server=MyServer;Database=MyDatabase;Integrated Security=True; Timeout=30";
         databaseConnection = new SqlConnection(source);

         // usp_InsertUser
         sqlCmd_usp_InsertUser = new SqlCommand("usp_InsertUser", databaseConnection);
         sqlCmd_usp_InsertUser.CommandType = CommandType.StoredProcedure;

         sqlCmd_usp_InsertUser.Parameters.Add(new SqlParameter("@UserID", SqlDbType.NChar, 8) { Direction = ParameterDirection.Input });
         sqlCmd_usp_InsertUser.Parameters.Add(new SqlParameter("@Department", SqlDbType.NChar, 4) { Direction = ParameterDirection.Input });
         sqlCmd_usp_InsertUser.Parameters.Add(new SqlParameter("@ReturnCode", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue });
     }


     public static void InsertTracking(string UserID, string Department)
     {
         try
         {
             if (databaseConnection.State != ConnectionState.Open)
                 databaseConnection.Open();
             sqlCmd_usp_InsertUser.Parameters["@UserID"].Value = UserID;
             sqlCmd_usp_InsertUser.Parameters["@Department"].Value = Department;
             sqlCmd_usp_InsertUser.ExecuteNonQuery();
             intReturnCode = Convert.ToInt16(sqlCmd_usp_InsertUser.Parameters["@ReturnCode"].Value);
             databaseConnection.Close();
         }
         catch (Exception ex)
         {
             DatabaseStatus = ex.Message;
         }
     }

     public static int InitializeDatabase()
     {
         try
         {
             SqlCommand cmdSQL = new SqlCommand("usp_InitializeDatabase", databaseConnection);
             cmdSQL.CommandType = CommandType.StoredProcedure;
             cmdSQL.Parameters.Add(new SqlParameter("@ReturnCode", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue });

             if (databaseConnection.State != ConnectionState.Open)
                 databaseConnection.Open();

             cmdSQL.ExecuteNonQuery();
             intReturnCode = Convert.ToInt16(cmdSQL.Parameters["@ReturnCode"].Value);

             databaseConnection.Close();
         }
         catch (Exception ex)
         {
             DatabaseStatus = ex.Message;
         }
         return intReturnCode;
     }
 }
于 2013-10-06T09:47:24.203 回答