gpt4 book ai didi

c# - 连接 .mdf 文件并读取

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:47 29 4
gpt4 key购买 nike

我有 .mdf 文件。

  • 数据库名称 -
  • .mdf 文件路径 - D:\db\People.mdf

接下来我尝试连接到 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());
}
}

并得到错误:

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

最佳答案

关于c# - 连接 .mdf 文件并读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014166/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com