数据库连接错误 - 第一次异常

Database connection error - first-chance exception

我在 VS2013 中有以下用于数据库连接的 C# 代码,当我 运行 收到以下错误消息时,我该如何解决它。

private void Form1_Load(object sender, EventArgs e)
{
    //using (SqlConnection conn = new SqlConnection())
    //{
    try
    {
        //con = new System.Data.SqlServerCe.SqlCeConnection();
        conn = new System.Data.SqlClient.SqlConnection();
        conn.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\vs2013projects\WFDBApp\WFDBApp\Dbapp.mdf;Integrated Security=True";
        conn.Open();
        MessageBox.Show("Openned database well");
        conn.Close();
    }
    catch
    {
        MessageBox.Show("failed to connect");
        //conn.Close();
    }
    //}
}

错误信息:

'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'c:\vs2013projects\WFDBApp\WFDBApp\bin\Debug\WFDBApp.exe'. Symbols loaded. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll The thread 0x1788 has exited with code 259 (0x103). The thread 0x1428 has exited with code 259 (0x103). The program '[7048] WFDBApp.vshost.exe' has exited with code 0 (0x0).

该错误很可能是由不正确的连接字符串引起的。 SQL Server Express 文件的连接字符串有几个规范示例。mdf(请参阅 MSDN 发布:https://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx):

使用 localDB 语法:

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Server=(LocalDB)\v11.0;Initial File Name=|DataDirectory|\DatabaseFileName.mdf;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=True" />

其他可能的选项如下所示:

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />

Select 与您的情况相关的那个,并确保您使用的是正确的语法。

此外,请记住数据库(基于文件的 SQL Express DatabaseFileName.mdf)可能受密码保护:如果是这样,那么您必须将此信息包含到连接字符串中(通常出于安全目的,此类连接字符串存储在 .config 文件中)。在后面的常规代码中,您应该使用以 @ 开头的逐字字符串(代替双反斜杠“\”)。

希望这可能有所帮助。最好的问候,