Answered by:
Connection String in C#

Question
-
Hello,
I am very new to using database in c#. The above code-I guess- is open a connection to the database which is named "selfNotes.sdf".
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=|DataDirectory|\\selfNotes.sdf;Password=pass;Persist Security Info=True";
conn.Open();
I copied the connection string from the database properities page as I don't know how to use it. And when I run the code I get the error message"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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)". Can anyone explain what this error message means and help me?
Thanks in advance
Tuesday, August 21, 2012 11:10 AM
Answers
-
Take a look at this blog
Steps to troubleshoot SQL connectivity issues
Also I would recommend that you use the "Connection String Wizard" it has the ability to do a Test Connection.
http://msdn.microsoft.com/en-us/library/w4dd7z6t.aspx
Mike
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 12:35 PM -
Hi,
One things I would like to ask you is that did you check your file is there or not? From your connection string it seems you are trying to connect with your data base file. Database file should be in your app running directory.
I guess this could be one problem for it.
Thanks and Regards, Shailesh B. Davara
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 1:30 PM -
Also, you need to specify the Data Source, like so:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = @".\SQLEXPRESS"; builder.AttachDBFilename = @"C:\SQL\NORTHWND.MDF"; builder.Password = "pass"; builder.PersistSecurityInfo = true; builder.UserInstance = true; builder.IntegratedSecurity = true; builder.ConnectTimeout = 30; //see if it is correct Console.WriteLine(builder.ConnectionString); Console.ReadLine(); } } }
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 2:13 PM
All replies
-
you need to put the password within quotes ('pass').
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=|DataDirectory|\\selfNotes.sdf;Password='pass';Persist Security Info=True"; conn.Open();
regards
joon
Tuesday, August 21, 2012 11:40 AM -
I put but it still gives the same error. Ty anyway.
odalgic
Tuesday, August 21, 2012 11:44 AM -
Please check the following URL
http://www.connectionstrings.com/sql-server-2005-ce
With Thanks and Regards
Sambath Raj.C
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming!- Proposed as answer by ryguy72 Saturday, October 13, 2012 5:45 PM
Tuesday, August 21, 2012 11:52 AM -
Take a look at this blog
Steps to troubleshoot SQL connectivity issues
Also I would recommend that you use the "Connection String Wizard" it has the ability to do a Test Connection.
http://msdn.microsoft.com/en-us/library/w4dd7z6t.aspx
Mike
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 12:35 PM -
Hi,
One things I would like to ask you is that did you check your file is there or not? From your connection string it seems you are trying to connect with your data base file. Database file should be in your app running directory.
I guess this could be one problem for it.
Thanks and Regards, Shailesh B. Davara
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 1:30 PM -
Also, you need to specify the Data Source, like so:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = @".\SQLEXPRESS"; builder.AttachDBFilename = @"C:\SQL\NORTHWND.MDF"; builder.Password = "pass"; builder.PersistSecurityInfo = true; builder.UserInstance = true; builder.IntegratedSecurity = true; builder.ConnectTimeout = 30; //see if it is correct Console.WriteLine(builder.ConnectionString); Console.ReadLine(); } } }
- Marked as answer by Bob Shen Monday, September 3, 2012 9:22 AM
Tuesday, August 21, 2012 2:13 PM