首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过SqlHelper类建立连接

无法通过SqlHelper类建立连接
EN

Stack Overflow用户
提问于 2013-07-16 17:44:14
回答 2查看 1K关注 0票数 0
代码语言:javascript
复制
   string strCon =
           ConfigurationManager.ConnectionStrings["constr"].ConnectionString;


           Company.Common.SqlHelper.Connect = strCon;

得到这个例外的人,请帮助

代码语言:javascript
复制
"The type initializer for 'Company.Common.SqlHelper' threw an exception."}

代码语言:javascript
复制
  /// <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);
                }
            }
        }
    }

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-16 17:46:35

问题可能在于您的配置文件中没有名称为Constr的连接字符串,因为根据错误描述,问题应该在这一行上。

代码语言:javascript
复制
public static string Connect = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString.ToString();
票数 0
EN

Stack Overflow用户

发布于 2013-07-16 17:46:52

SqlHelper的类型初始化程序似乎正在做的唯一一件事是:

代码语言:javascript
复制
public static string Connect = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString.ToString();

这是什么例外?如果我猜的话,配置中可能没有一个名为Constr的连接字符串。在那里吗?你能给我们显示配置设置吗?否则,什么是例外呢?

还请注意,在这一行中不需要使用.ToString()ConnectionString已经是一个字符串了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17683503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档