C# SqlConnection Failed

Question:

      SqlConnection ABC = new SqlConnection(
        "Data Source=(local);Initial Catalog=Database1;Integrated Security=SSPI");
    SqlCommand command = new SqlCommand();
    SqlDataReader dataRead;

I'm using this code to make a save button on a form to move offline and say at the beginning

Warning 1 The field 'WindowsFormsApplication1.Form3.dataRead' is never used C:…WindowsFormsApplication1\Form3.cs 23 23 WindowsFormsApplication1

and when pressing save this error occurs.

Network or instance-specific error connecting to SQL Server. The server was not found or was not reachable. Check that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Unable to open a connection to SQL Server)

my base date is on

Data Source=C:\Users\Somline\Documents\Visual Studio 2008\Projects\PROGRAM\WindowsFormsApplication1\Database1.sdf;Persist Security Info=True

Answer:

This here is not a mistake. It's a warning (warning):

Warning 1 The field 'WindowsFormsApplication1.Form3.dataRead' is never used C:…WindowsFormsApplication1\Form3.cs 23 23 WindowsFormsApplication1

It's just saying that you're not using the given component anywhere in your code.

About the connection, the connection string is wrong. If you want to point the connection string to an SDF file, the correct one is:

SqlCeConnection ABC = new SqlConnection(
    "Data Source=C:\Users\Somline\Documents\Visual Studio 2008\Projects\PROGRAMA\WindowsFormsApplication1\Database1.sdf;Persist Security Info=False");
SqlCeCommand command = new SqlCommand();
SqlCeDataReader dataRead;

Note that the classes are SQL Server Compact, not regular SQL Server ( SqlCeConnection , SqlCeCommand , SqlCeDataReader ). It has Ce in the middle of the name.

Scroll to Top