Search code examples
c#.netado.netsqlconnectionsqlclient

Database connection error - first-chance exception


i have the following C# code for database connection in VS2013, which when i run i get the following error message, how can i get around it.

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();
    }
    //}
}

error message:

'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).


Solution

  • The error is most likely caused by incorrect connection string. There are several canonical samples of connection string to SQL Server Express file .mdf (see MSDN pub: https://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx):

    Using localDB syntax:

    <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" />
    

    Other possible options are shown below:

    <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 the one pertinent to your case and make sure you are using correct syntax.

    Also, keep in mind that Database (that file-based SQL Express DatabaseFileName.mdf) could be password-protected: if so, then you have to include this info into connection string (typically such connection string is stored in .config file for security purpose). In the regular code behind you should use a verbatim string starting with @ (in lieu of double backslash "\\").

    Hope this may help. Best regards,