在代码中生成数据库 [英] Generate database in code

查看:66
本文介绍了在代码中生成数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

 

我有一个应用程序,我想放在几个不同的服务器上。  当用户选择安装数据库时,我希望我的应用程序创建一个包含所有表等的空数据库。  在此之前,用户必须在几个不同的编辑框中输入所有
连接字符串的各个部分。  用户还可以选择将数据库的实际名称更改为他选择的名称。  可以在同一台服务器
上使用不同名称的多个这些数据库。  我知道有一种方法可以在Visual Studio 2010设计器中创建数据库,但我真的希望让我的应用程序能够在代码中执行此操作。

I have a app that I would like to place on several different servers.  I would like my app to create a empty database with all the tables, etc when the user selects to install the database.  Before this happens the user will have to put in all the various pieces of the connection string in a few different edit boxes.  The user will also have the option of changing the actual name of the database to what he chooses.  It would be possible to have several of these databases on the same server with different names.  I know there is a way to create the database in the Visual Studio 2010 designer but I would really like to have my app be able to do this in code.

有人能告诉我一些代码或链接会显示如何执行此操作?

Could someone show me some code or a link that would show how to do this?

 

谢谢!

推荐答案

您好Lavagin,

Hi Lavagin,

我假设您使用的是Visual Studio 2010和.NET Framework 4.0附带的EF4?如果是这样,那么您可以在ObjectContext上使用CreateDatabaseScript方法;

http://msdn.microsoft。 com / en-us / library / system.data.objects.objectcontext.createdatabasescript.aspx

I assume you are using EF4 that is included with Visual Studio 2010 and .NET Framework 4.0? If so then you can use the CreateDatabaseScript method on your ObjectContext; http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.createdatabasescript.aspx

~Rowan


这篇关于在代码中生成数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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