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:

  1. 如果在連接字串中識別了資料庫,就會使用它的名稱。If a database is identified in the connection string, its name is used.
  2. 如果有Name屬性,則會使用它的屬性做為資料庫的名稱。 DatabaseAttributeIf a DatabaseAttribute attribute is present, its Name property is used as the name of the database.
  3. 如果連接字串中沒有資料庫標記,而且使用了強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.
  4. 如果使用弱式DataContext型別,則會擲回例外狀況。If a weakly typed DataContext is used, an exception is thrown.
  5. 如果是DataContext使用檔案名建立的,則會建立對應于該檔案名的資料庫。If the DataContext has been created by using a file name, the database corresponding to that file name is created.

適用於

另請參閱