DataContext.CreateDatabase 方法
定义
在服务器上创建数据库。Creates a database on the server.
public:
void CreateDatabase();
public void CreateDatabase ();
member this.CreateDatabase : unit -> unit
Public Sub CreateDatabase ()
示例
下面的代码演示如何设置临时数据库,然后将其删除。The following code shows how to set up a temporary database and then remove it.
if (!db.DatabaseExists())
db.CreateDatabase();
// …
db.DeleteDatabase();
If Not db.DatabaseExists Then
db.CreateDatabase()
End If
' ...
db.DeleteDatabase()
注解
该数据库的名称是使用以下算法派生的:The name of the database is derived by using the following algorithm:
- 如果在连接字符串中标识数据库,则使用其名称。If a database is identified in the connection string, its name is used.
- 如果 DatabaseAttribute 存在属性,则该属性 Name 将用作数据库的名称。If a DatabaseAttribute attribute is present, its Name property is used as the name of the database.
- 如果连接字符串中没有数据库标记,并且使用强类型,则将 DataContext 创建一个与继承类同名的数据库 DataContext 。If there is no database tag in the connection string and a strongly typed DataContext is used, a database that has the same name as the DataContext inheriting class is created.
- 如果使用弱类型 DataContext ,则会引发异常。If a weakly typed DataContext is used, an exception is thrown.
- 如果已 DataContext 使用文件名创建了,则将创建与该文件名称对应的数据库。If the DataContext has been created by using a file name, the database corresponding to that file name is created.