Share via


Connection String in C#

Question

Tuesday, August 21, 2012 11:10 AM

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;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

All replies (6)

Tuesday, August 21, 2012 12:35 PM ✅Answered | 1 vote

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


Tuesday, August 21, 2012 1:30 PM ✅Answered

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


Tuesday, August 21, 2012 2:13 PM ✅Answered

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

Tuesday, August 21, 2012 11:40 AM

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:44 AM

I put but it still gives the same error. Ty anyway.

odalgic


Tuesday, August 21, 2012 11:52 AM

Please check the following URL

http://www.connectionstrings.com/sql-server-2005-ce

With Thanks and Regards
Sambath Raj.C
click &quotProposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming!