【问题标题】:fix The ConnectionString property has not been initialized修复 ConnectionString 属性未初始化
【发布时间】:2020-02-17 21:41:19
【问题描述】:

当我启动我的应用程序时,我收到此错误:

ConnectionString 属性尚未初始化。

我在尝试连接到我的 SQL Server 数据库时遇到此异常。

注册.aspx.cs

protected void btnRegister_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = string.Format("insert into member 
values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
            txtFullName.Text, rblGender.SelectedValue, ddlCountry.SelectedValue,
            txtPhone.Text, txtEmail.Text, txtUsername.Text, txtPassword.Text);

    try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        tblRegister.Visible = false;
        Response.Write("Your account is created.");
    }
    catch(SqlException ex)
    {
        if (ex.Number == 2627)
            lblMsg.Text = "Please Change The Username.";
        else
            lblMsg.Text = "An Error : " + ex.Message; 
    }
}

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.6.1"/>
        <httpRuntime targetFramework="4.6.1"/>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs"
                      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, 
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                      warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
                      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, 
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                      warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
    </system.codedom>
    <appSettings>
        <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
    <connectionStrings>
        <add name="MyDB"
             connectionString="Data Source=.\sqlexpress;Initial Catalog=Company;Integrated Security=True"
             providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

【问题讨论】:

  • 错误信息很清楚。您已经创建了一个 SqlConnection,但您没有告诉这个对象要使用的连接字符串是什么。

标签: sql asp.net


【解决方案1】:

你需要这样做:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=MSSQL1;Initial Catalog=AdventureWorks;Integrated Security=true;"";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

使用 ConnectionString 您可以指定要连接到哪个服务器以及使用什么凭据,否则它不会知道您要连接到哪里以及使用什么。 我给出的示例是针对 SQL 服务器和集成安全性的,具体取决于您的数据库、服务器和您的凭据,您需要找到所需的连接字符串

【讨论】:

    猜你喜欢
    • 2021-06-23
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多