如何创建“嵌入式”应用程序SQL 2008 数据库文件如果不存在?
我使用 C#、ADO.Net 和在 Server Management Studio 中创建的嵌入式 MS SQL 2008 数据库文件(附加到 MS SQL 2008 Express)创建了一个数据库应用程序。有人可以向我指出一个资源,该资源描述了如何以编程方式创建数据库文件(如果数据库文件丢失)(例如在安装我的应用程序后)?
I've created a database application using C#, ADO.Net and an embedded MS SQL 2008 database file (that attaches to MS SQL 2008 Express) which I created in Server Management Studio. Can someone point me to a resource that describes how I can programmatically create the database file if it is missing (like right after my application is installed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果是我(当是我时......):
您并不特别想尝试通过复制和附加它们来使数据库文件工作 - 您可能有原因想要这样做,但我相信这些都是例外而不是规则。
因此,您需要做的是编写数据库创建脚本,即使用 SQL DDL 来创建数据库和表以及模式中的所有其他内容。
几乎您需要执行此操作的是对服务器实例的适当权限,然后是连接字符串(您可以将其与服务器/实例名称分开构建)。
从这里:
从代码上看:判断数据库是否存在的方法;方法创建一个标准的“空”数据库,其版本表和版本号为0;方法通过运行适当的 DDL 将架构升级到当前版本(我们将我们的 DDL 编码为 C#,因为它提供了更大的灵活性,但您同样可以按顺序运行 DDL 脚本)。
它是否存在:
创建一个新数据库:
更新代码有点复杂,但基本上运行如下内容:
每次更新都包含在一个事务中 - 因此,如果更新失败,您应该让数据库处于已知的良好状态。
为什么要这样做(在代码中,这并非没有经过试验?)最终的结果是高度确信您的应用程序正在对话的模式是您的应用程序期望与之对话的模式......正确的表,正确的列(以正确的顺序,即正确的类型和正确的长度)等等,并且随着时间的推移,这种情况将继续存在。
抱歉,如果这有点长 - 但这是我非常热衷的事情......
If it were me (when it is me...):
You don't particularly want to be trying to make database files work by copying them and attaching them - there are reasons why you might want to but I believe these to be exceptions rather than rules.
Accordingly what you need to do is to script creation of the database i.e. to use SQL DDL to create the database and the tables and all the other stuff in your schema.
Pretty much all you need to enable you to do this is appropriate rights to the server instance and then a connection string (which you can probably build apart from the server/instance name).
From here:
From a code point of view: method to determine if a database exists; method to create an standard "empty" database with a version table and a version number of 0; methods to bring the schema up to the current version by running the appropriate DDL (we encode ours into C# because it provides more flexibility but you could equally run DDL scripts in sequence).
Does it exist:
Create a new database:
The update code is a tad more complex but basically runs stuff like this:
All wrapped up in a transaction per update - so that if the update fails you should leave the database is a known good state.
Why do it this way (in code, which is not without its trials?) well the end result is a high degree of confidence that the schema your app is talking to is the schema your app expects to talk to... right tables, right columns (in the right order, that are the right type and the right length), etc, etc. and that this will continue to be the case over time.
Apologies if this is a bit long - but this is something I'm quite keen on...
如果“嵌入式 MS SQL”是“Microsoft SQL Server Compact 3.5”:
If the "embedded MS SQL" is a "Microsoft SQL Server Compact 3.5":