且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用C#创建SQL Server数据库和表

更新时间:2023-02-07 11:29:26

您可以使用
ServiceController
 检查"SQLBrowser"是否为"SQLBrowser"。服务是否存在以确定是否安装了SQL2005或更高版本的服务实例(即使只安装了单个SQL服务器实例,SQL浏览器仍然存在
但处于"禁用"状态)

You can use ServiceController to check if "SQLBrowser" service exists or not to find out whether a SQL2005 or later version of service instance is installed (even if only a single instance of SQL server is installed, the SQL Browser will still be there but in "disabled" state)

在验证了localhost上至少安装了一个SQL服务器实例后,你可以

获取实例名称

SqlDataSourceEnumerator
类。 (如果运行SqlBrowser的本地子网上有其他计算机并响应您的enquery,则可能会返回结果)

After verified at least one SQL server instance is installed on localhost, you can get the instance name with SqlDataSourceEnumerator class. (This may return result if there is other machine on local subnet running SqlBrowser and respond to your enquery)

现在您可以尝试连接到该实例,或尝试使用ServiceController启动它再次,如果它没有运行,或显示可用实例的列表,如果有多个(按任何顺序你想要它)。然后应用脚本创建数据库
或根据需要更新表。

Now you can try to connect to the instance, or try to bring it up with ServiceController again if it's not running, or show a list of available instances if there is more than one (in any order you want it to be). And then apply the scripts to create database or update tables as you wish.