gpt4 book ai didi

c# - 从 C# 代码与 SQL Server 2014 Express 的连接不起作用,无法打开连接

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

我正在使用 C# 在 WPF 中创建一个客户端,我需要将它安装在客户端(平板电脑表面)上。也就是说,我安装了 SQL Server 2014 Express 版本,只是为了获取实例,我从这里下载:

https://www.microsoft.com/en-US/download/details.aspx?id=42299

我明白了:

Express 64BIT\SQLEXPR_x64_ENU.exe

我安装了一切,当然我没有SQL Server界面的管理,但是我不能创建数据库,这是我的错误:

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

这是我的连接字符串(从常量文件中获取):

Server=SQLEXPRESS;Database=master;Integrated security=true;

这是我的代码:

 public void CreateDatabase(String PID)
{
String connection = Constants.localServerConnectionSQL.LocalServerConnectionSQLName;
SqlConnection Connection = new SqlConnection();
Connection = new SqlConnection(connection);
Connection.Open();

string Path = Environment.GetEnvironmentVariable("LocalAppData") + @"\CDA\UserDatabase\" + PID.ToString();
log.Info("DBCreationScripts: Path DB: " + Path.ToString());

String str = "CREATE DATABASE [" + PID + "] ON PRIMARY " +
"(NAME = MyDatabase_Data, " +
"FILENAME = '" + Path + ".mdf', " +
"SIZE = 5MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = [" + PID + "Log_Log], " +
"FILENAME = '"+Path+"Log.ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";

log.Info("DBCreationScripts: Comando di creazione DB: " + str);


try
{
SqlCommand myCommand = new SqlCommand(str, Connection);
log.Info("DBCreationScripts: Lancio il comando di creazione database");
myCommand.ExecuteNonQuery();
}
catch (System.Exception ex)
{
log.Info("DBCreationScripts: Eccezione nel comando di creazione database - Message: " + ex.Message);
throw ex;
}
}

问题是打开连接失败,实例是SQLEXPRESS,但是不明白为什么同事没有。 E ' 我可以配置错误吗?

SQL 服务都处于事件状态。

提示?

最佳答案

引用 SQL Server 实例的正确方法是 Server Address\Instance Name,例如 MyServer\SQLExpress.\SQLEXPRESS127.0.0.1\SQLEXPRESS

如果没有反斜杠,文本将被解释为机器名称。带有 Server=SQLEXPRESS 的连接字符串将尝试连接到名为 SQLEXPRESS 的服务器。

关于c# - 从 C# 代码与 SQL Server 2014 Express 的连接不起作用,无法打开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173008/

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