Coffeeman
Coffeeman

Reputation: 295

Connect .mdf file and read

I have .mdf file.

Next I try connct to mdf

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

And get Error:

"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)"

Upvotes: 2

Views: 1539

Answers (1)

Related Questions