2013-07-16 50 views
0
string strCon = 
      ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 


      Company.Common.SqlHelper.Connect = strCon; 

獲取建立連接這個例外有人幫不能夠通過提供SQLHelper類

"The type initializer for 'Company.Common.SqlHelper' threw an exception."} 

/// <summary> 
/// The SqlHelper class is intended to encapsulate high performance, scalable best practices for 
/// common uses of SqlClient 
/// </summary> 
public sealed class SqlHelper 
{ 
    public void MessageBox(string message, Control ControlID) 
    { 

     //tmp = "<script language='javascript'>"; 
     string tmp = "alert('" + message + "');"; 
     //tmp += "</script>"; 
     //ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp", tmp, true); 
     //string CloseWindow; 
     //CloseWindow = "alert('Hello World')"; 
     ScriptManager.RegisterStartupScript(ControlID, ControlID.GetType(), "tmp", tmp, true); 
     // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", tmp); 
    } 
    public static string Connect = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString.ToString(); 
    #region private utility methods & constructors 

    // Since this class provides only static methods, make the default constructor private to prevent 
    // instances from being created with "new SqlHelper()" 
    private SqlHelper() { } 

    /// <summary> 
    /// This method is used to attach array of SqlParameters to a SqlCommand. 
    /// 
    /// This method will assign a value of DbNull to any parameter with a direction of 
    /// InputOutput and a value of null. 
    /// 
    /// This behavior will prevent default values from being used, but 
    /// this will be the less common case than an intended pure output parameter (derived as InputOutput) 
    /// where the user provided no input value. 
    /// </summary> 
    /// <param name="command">The command to which the parameters will be added</param> 
    /// <param name="commandParameters">An array of SqlParameters to be added to command</param> 
    private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) 
    { 
     if (command == null) throw new ArgumentNullException("command"); 
     if (commandParameters != null) 
     { 
      foreach (SqlParameter p in commandParameters) 
      { 
       if (p != null) 
       { 
        // Check for derived output value with no value assigned 
        if ((p.Direction == ParameterDirection.InputOutput || 
         p.Direction == ParameterDirection.Input) && 
         (p.Value == null)) 
        { 
         p.Value = DBNull.Value; 
        } 
        command.Parameters.Add(p); 
       } 
      } 
     } 
    } 

}

+0

哪條線給你例外? – Ehsan

回答

0

也許問題是,有上沒有連接字符串您配置文件名稱爲Constr as,根據錯誤描述,問題應該在這一行

public static string Connect = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString.ToString(); 
0

SqlHelper類型初始化似乎做的唯一事情是這樣的:

public static string Connect = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString.ToString(); 

什麼例外的是該投擲?如果我猜測,配置中可能沒有連接字符串Constr。在那兒?你可以向我們展示配置設置嗎?否則,什麼是例外?

還要注意,.ToString()沒有必要在該行。 ConnectionString已經是一個字符串。