2015-05-03 1 views
2

У меня есть .mdf файл.Подключить файл .mdf и прочитать

  • имя DataBase - люди
  • .mdf путь к файлу - D: \ DB \ People.mdf

Следующая Стараюсь CONNCT к МДФ

DbProviderFactory df = DbProviderFactories.GetFactory("System.Data.SqlClient"); 

using (DbConnection cn = df.CreateConnection()){ 
    Console.WriteLine("Your connection object is a: {0}", cn.GetType().Name); 
    cn.ConnectionString = 
    @"Data Source=(local);AttachDbFilename=D:\db\People.mdf;"+ 
     "Initial Catalog=People;Integrated Security=True"; 
    cn.Open(); 
    DbCommand cmd = df.CreateCommand(); 
    Console.WriteLine("Your command object is a: {0}", cmd.GetType().Name); 
    cmd.Connection = cn; 
    cmd.CommandText = "Select * From priluki"; 

    using (DbDataReader dr = cmd.ExecuteReader()){ 
    while (dr.Read()) 
     Console.WriteLine("-> {0} {1}", 
     dr["first_name"].ToString(), dr["last_name"].ToString()); 
    } 
} 

И получить ошибку:

"An unhandled exception of type "System.Data.SqlClient.SqlException" in System.Data.dll

For more information: When connecting to SQL Server error occurred with the network or to a specific instance. The server was not found or is not available. Verify that the instance name is correct and that SQL Server to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

+0

Ваше приложение не может установить соединение с базой данных. [Строки подключения] (https://www.connectionstrings.com/sql-server/) –

+0

Вы уверены, что начальный каталог = правильный синтаксис? Попробуйте Database = – Paparazzi

+0

скажите мне, вы уверены, что файл mdf имеет правильные разрешения? –

ответ

Смежные вопросы