Hi all,

i am trying some code examples given in a book. I am getting an error while connecting to the database.I have installed sqlexpress edition and trying to connect to system database "master".

Below is 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

And here is the code which i am trying.

class Program
    {
        static void Main(string[] args)
        {
            // Create a connection string builder
            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

            // Define connection string attributes using three techniques
            csb.DataSource = "(local)";
            csb.Add("Initial Catalog", "master");
            csb["Integrated Security"] = true;

            // Output the connection string from the connection string builder
            Console.WriteLine("Connection string:\n{0}", csb.ConnectionString);

            // Create a connection string from the connection string builder
            SqlConnection connection = new SqlConnection(csb.ConnectionString);
            // Open and close the connection
            connection.Open();
            Console.WriteLine("\nConnectionState = {0}", connection.State);
            connection.Close();
            Console.WriteLine("ConnectionState = {0}", connection.State);

            Console.WriteLine("\nPress any key to continue.");
            Console.ReadKey();
        }
    }

pls help me...

Thanks

Here is an example of connecting to an SQL Database, executing a query, and loading a DataTable:

private void button1_Click(object sender, EventArgs e)
    {
      const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      const string query = "Select * From Invoice Where InvNumber = @InvNumber";
      using (DataTable dt = new DataTable())
      {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
          conn.Open();
          using (SqlCommand cmd = new SqlCommand(query, conn))
          {
            cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100));
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
              dt.Load(dr);
            }
          }
          conn.Close();
        }
        System.Diagnostics.Debugger.Break();
      }
    }

By default SQL does not enable network protocols which results in the error message you are seeing. Here is the parameters to install SQL 2005 express (the password needs to be changed, i set it as p@ssw0rd):

"setup.exe" /qb ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_Replication,Client_Components,Connectivity,SQL_SSMSEE INSTANCENAME=SqlExpress SAVESYSDB=1 SQLBROWSERAUTOSTART=1 SQLAUTOSTART=1 AGTAUTOSTART=1 SECURITYMODE=SQL SAPWD=p@ssw0rd DISABLENETWORKPROTOCOLS=0 ERRORREPORTING=0 SQMREPORTING=0 ADDUSERASADMIN=1 INSTALLSQLDIR="%ProgramFiles%\Microsoft SQL Server\"

SQL 2005 Express enable remote connections:
http://support.microsoft.com/kb/914277

SQL 2008 Express enable remote connections:
http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/

plus don't forget to check up network settings - firewalls and antivirus can do funny things

Start SQL server instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.