1

I'm using Visual C# Express 2008 and created a database and dataset by going to Add->New Data Source. I'm trying to add a record to it but cannot connect using the data string provided in the wizard. Please help. Here is my code

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=|DataDirectory|\\myLife.sdf;Password=*******");

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT profiles (profile, file) VALUES ('here', 'here')";
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
2
  • 2
    What is the error? Is the database file present? Is it in the correct location? Is it writable by the applicable user context?
    – David
    Commented Oct 13, 2010 at 18:51
  • A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
    – Ite Code
    Commented Oct 13, 2010 at 18:59

2 Answers 2

1

Since you are using SQL Compact Edition database file (.sdf) you should use SqlCeConnection instead of SqlConnection as explained in this other post

0

Normally, you need to specify the username and the password in the same connection string. I know that you can use your windows credentials to connect by using integrated security=true in the connection string, but I think that in your case, you probably want to use Data Source=|DataDirectory|\\myLife.sdf;User id=Bob;Password=*******

2
  • A good site for building your connection strings is... connectionstrings.com
    – Patrick
    Commented Oct 13, 2010 at 18:56
  • I'm not sure that SQL Server CE supports user-level logon security, but, I believe, the DB itself can be password protected.
    – Steven
    Commented Oct 13, 2010 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.