如何在C#中编程创建SQL

如何在C#中编程创建SQL

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

问题描述

我想创建一个程序,它将使用SQL Server 2008数据库。在第一次启动时,我想打开一个窗口,让用户创建一个数据库。它将简单地,文本框与数据库计算机的名称和ip设置和按钮Go。

I want to create a program, that will use SQL Server 2008 database. During the first start, I would like to open a window, which let the user create a database. It will be simply, textboxes with name and ip of the database computer to set and button "Go".

程序将在WPF .NET4中。而数据库将在本地网络。

Program will be in WPF .NET4. And database will be in local network.

你能建议我一个好的解决方案吗?这是一个好的编程实践,要做到吗?或者也许我应该附加一个sql脚本?

Could you suggest me a good solution? Is it a good programming practice, to do that? Or maybe I should just attached a sql script?

我做了一些研究,我发现这篇文章:

I do some research, I found that article: http://www.codeproject.com/KB/tips/CreateSQLDV.aspx

但是,第一个问题,在SQL Server 2008中,没有 Microsoft.SqlServer.SmoEnum.dll 。所以,当我做类似的数据创建窗口,但对于SQL Server 2008(使用也许不同的dll) - 它不能用于SQL Server 2005.也许不能使用SQL Server 2008的其他版本?我不知道..从codeproject的例子看起来不错,但我不知道。

But, first issue, in SQL Server 2008, there is no Microsoft.SqlServer.SmoEnum.dll. So, when I do similar "data create" window, but for SQL Server 2008 (using maybe different dlls) - It will not work for SQL Server 2005. And maybe will not work with other versions of SQL Server 2008 to? I don't know.. Example from codeproject looks good, but I'm not sure.

我想做一个灵活的程序。

I would like to do a flexible program.

推荐答案

我会避免SMO。

你的受众和你对预期环境的控制,但附加预制数据库,而一个方便的选择,有时可能有问题 - 开始它是一个二进制源代码控制,所以你不能得到免费的源控制系统。此外,您附加的数据库具有特定的选项和事情,可能不适合特定的目标环境 - SQL Server 2005,SQL Server 2008,SQL Server 2008 R2?除此之外,它是一种类似于过去部署Access应用程序的有效方法。

It depends a LOT on your audience and the control you have over the expected environment, but attaching pre-made databases, while a convenient option, can sometimes have issues - to start with it's a binary under source control, so you don't get diffs for free in your source control system. In addition, you're attaching a database with certain options and things which might not be appropriate for the specific target environment - SQL Server 2005, SQL Server 2008, SQL Server 2008 R2? Other than all that, it's a valid approach similar to the way one might deploy Access applications in the past.

在一个不受控制的环境中,我会使用生成SQL脚本包含所有DDL(和用于查找表的DML)或提供一个脚本,提供自动运行它,并且让他们自己使用自己的工具(如果他们有一个DBA)运行它。

In a less controlled environment, I would go with either generating a SQL script containing all the DDL (and DML for lookup tables) or providing a script, offering to run it automatically and also giving them the option of running it themselves with their own tools (if they have a DBA).

现在你的脚本(或至少脚本的模板或生成脚本的代码)在源代码控制下,可以满足需要检查它的DBA。

Now your script (or at least the template for the script or the code that generates the script) is under source control and can satisfy a DBA who wants to inspect it.

这篇关于如何在C#中编程创建SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:10