Mike Jonson

Mike Jonson

  • NA
  • 239
  • 193.6k

Error 40 connect with DB

Jul 11 2011 4:42 AM
Have next config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="provider" value="System.Data.SqlClient" />
    <add key="cnStr" value="Data Source=localhost:uid=sa;pwd=;Initial Catalog=NORTHWND;Integrated Security=True;" />
  </appSettings>
</configuration>

And have next code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;

namespace DataProviderFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("******Fun with Data provider factories******");
            string dp = ConfigurationManager.AppSettings["provider"];
            string cnStr = ConfigurationManager.AppSettings["cnStr"];

            DbProviderFactory df = DbProviderFactories.GetFactory(dp);
            DbConnection con = df.CreateConnection();
            Console.WriteLine("Your connection  object is {0}", con.GetType().FullName);

            con.ConnectionString = cnStr;
            con.Open();

            DbCommand com = df.CreateCommand();
            Console.WriteLine("Your comand  object is {0}", com.GetType().FullName);

            com.Connection = con;
            com.CommandText = "SELECT * FROM Debitors";

            DbDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
            Console.WriteLine("Your data reader object is  a: {0}", dr.GetType().FullName);

            Console.WriteLine("\n****Debitors****");
            while (dr.Read())
                Console.WriteLine(dr["DebitorName"]);
            dr.Close();

        }
    }
}

But cant conect with date base, every time error:
When connecting to the SQL Server error associated with the network or with a specific instance. Server not found or is unavailable. Make sure that the instance name is correct and that SQL Server allows remote connections. (provider: named pipes provider, error: 40-could not open connection to SQL Server)

Please, help, why?





Answers (4)