gpt4 book ai didi

c# - 使用 SMO 在 SQL Server Express 中创建数据库

转载 作者:行者123 更新时间:2023-11-30 14:00:13 24 4
gpt4 key购买 nike

我正在尝试使用 WinForms 和 C# 在 SQL Server Express 中创建一个数据库

这是我想做的

Microsoft.SqlServer.Management.Smo.Server srv = new Microsoft.SqlServer.Management.Smo.Server srvServer();
int i = srv.Databases.Count;

只是为了在开始时获得计数。但是我得到了错误

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) this is the stack track

at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetServerVersion()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean inServer)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer()
at Microsoft.SqlServer.Management.Smo.AbstractCollectionBase.get_StringComparer()
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCollection()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_InternalStorage()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollection(Boolean refresh)
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_Count()
at CreateDB.CreateDB.btnCreateDB_Click(Object sender, EventArgs e) in C:\Users\Guest1\Downloads\CreateDB\CreateDB\CreateDB.cs:line 82

应该怎么办?

最佳答案

如果您正在使用 SQL Server Express,并且您已经安装了所有默认设置,那么您的服务器实例将被称为 .\SQLEXPRESS。您需要在代码中使用它:

using Microsoft.SqlServer.Management.Smo;

Server srv = new Server(".\\SQLExpress");
int i = srv.Databases.Count;

如果您创建一个新的 Server 实例而没有指定实例名称,它会尝试连接到您没有的默认实例(没有名称) ,如果您只安装了 SQL Server Express。

关于c# - 使用 SMO 在 SQL Server Express 中创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11644603/

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