2012-08-08 74 views
1

如果我有一个dll,我在许多Web应用程序中使用我的应用程序中的dll中的方法。如何处理外部DLL中的连接字符串?

处理连接字符串的最佳做法是什么?

dll方法是静态方法..

这是我做的

private static String connectionString = "User Id=xxx;Password=xxx;Host=xxx;Server=xxx;Service=1525;Database=xxx; Client Locale=ar_ae.1256; Database Locale=ar_ae.8859-6; Protocol=olsoctcp;pooling=true;Min Pool Size=4;Max Pool Size=400;Connection Timeout=30"; 

public static int Is_Valid_User(string p_u, string p_p) 
     { 
      int ret = 0; // invalid user 
      using (IfxConnection conn = new IfxConnection(connectionString)) 
      { 
       IfxCommand DBCmd = new IfxCommand(); 
       String p = My_Decryption_2(p_p); 
       try 
       { 
        if (conn.State == ConnectionState.Closed) 
         conn.Open(); 
        DBCmd = new IfxCommand(); 
        DBCmd.Connection = conn; 
        DBCmd.CommandText = "SELECT nvl(emp_num,0) FROM emp_net WHERE username = ? AND DECRYPT_CHAR(password, 'xxxxxx') = ? "; 
        DBCmd.Parameters.Add("user_name", p_u); 
        DBCmd.Parameters.Add("password", p); 
        using (IfxDataReader dataReader = DBCmd.ExecuteReader()) 
        { 
         if (dataReader.Read()) 
          ret = (int)dataReader[0]; 
         dataReader.Close(); 
        } 
       } 
       catch (ApplicationException e) 
       { 

       } 
       conn.Close(); 
      } 
      return ret; 
     } 

这是在我的web应用程序中使用dll的好方法还是有比这更好的方法?

回答

1

你几乎用这些静态方法在脚下自己射击。 最好的做法是重构类以使用实例方法并通过构造函数或属性注入连接字符串。

失败,你可以重构静态方法来包含连接字符串?

public static int Is_Valid_User(string p_u, string p_p, string connectionString) 

您可以硬编码到[网络|应用]参考配置连接字符串太多,但已不仅改变了问题(在固定字符串的依赖),不就解决了。

1

它最好有连接字符串在某种配置,如web.config

+0

你是否意味着我需要添加一个web来配置我的'dll',除此之外我的web应用程序中的web.config? – 2012-08-08 09:09:39

+0

不,你不需要在dll中添加一个单独的web.config,一个dll可以读取Web应用程序中包含的web.config的连接字符串。 – 2012-08-08 09:49:20