用C#创建SQL Server数据库和表 [英] Making SQL Server databases and tables in C#

查看:272
本文介绍了用C#创建SQL Server数据库和表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的帖子可能有1000多个。只是找到我想要的那个将是一个痛苦。

There's more then likely 1000's of post like this. Just finding the one I want would be a pain.

我想要做的是检测SQL Server是否已安装。如果是,则创建数据库和表(如果它们不存在)。有人会知道如何在Windows窗体中编写代码。在C#中。已经很久了。在我退休之前,我一直想更新我的旧软件

What I want to do is detect that SQL Server is installed. if so create a database and tables if they don't exist. Would someone have an idea on just how to code this in windows forms. In C#. It's been a long time. I been wanting to update my old software before I retired it.

Joe

http://www.df-barracks .com Delta Force Barracks

http://www.starfiresoft.com Starfire Software

http://www.df-barracks.com Delta Force Barracks
http://www.starfiresoft.com Starfire Software

推荐答案

您可以使用
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.


这篇关于用C#创建SQL Server数据库和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆