SqlMetal.exe (程式碼產生工具)SqlMetal.exe (Code Generation Tool)

SqlMetal 命令列工具會為 .NET Framework 的 LINQ to SQLLINQ to SQL 元件產生程式碼及對應。The SqlMetal command-line tool generates code and mapping for the LINQ to SQLLINQ to SQL component of the .NET Framework. 藉由套用本主題稍後出現的選項,您就可以指示 SqlMetal 執行數個不同的動作,包括以下各項:By applying options that appear later in this topic, you can instruct SqlMetal to perform several different actions that include the following:

  • 從資料庫產生原始程式碼和對應屬性或對應檔。From a database, generate source code and mapping attributes or a mapping file.

  • 從資料庫產生中繼資料庫標記語言 (.dbml) 檔用於自訂。From a database, generate an intermediate database markup language (.dbml) file for customization.

  • 從 .dbml 檔產生程式碼和對應屬性或對應檔。From a .dbml file, generate code and mapping attributes or a mapping file.

此工具會自動與 Visual Studio 一起安裝。This tool is automatically installed with Visual Studio. 根據預設,這個檔案位於 drive:\Program Files\Microsoft SDKs\Windows\vn.nn\bin。By default, the file is located at drive:\Program Files\Microsoft SDKs\Windows\vn.nn\bin. 如果您沒有安裝 Visual Studio,您也可以下載 Windows SDK以取得 SQLMetal 檔案。If you do not install Visual Studio, you can also get the SQLMetal file by downloading the Windows SDK.

注意

使用 Visual Studio 的開發人員也可以使用 Object Relational DesignerObject Relational Designer 來產生實體類別。Developers who use Visual Studio can also use the Object Relational DesignerObject Relational Designer to generate entity classes. 命令列方法會針對大型資料庫做適當調整。The command-line approach scales well for large databases. 由於 SqlMetal 是命令列工具,因此您可以在建置處理序中使用它。Because SqlMetal is a command-line tool, you can use it in a build process.

若要執行這項工具,請使用 [Visual Studio 開發人員命令提示字元] (或 Windows 7 中的 [Visual Studio 命令提示字元])。To run the tool, use the Developer Command Prompt for Visual Studio (or the Visual Studio Command Prompt in Windows 7). 如需詳細資訊,請參閱命令提示字元。在命令提示字元中,鍵入下列命令:For more information, see Command Prompts.At the command prompt, type the following:

語法Syntax

sqlmetal [options] [<input file>]  

選項Options

若要檢視最新的選項清單,請進入安裝位置,並在命令提示字元輸入 sqlmetal /?To view the most current option list, type sqlmetal /? at a command prompt from the installed location.

連接選項Connection Options

選項Option 說明Description
/server: <名稱>/server: <name> 指定資料庫伺服器名稱。Specifies database server name.
/database: <名稱>/database: <name> 指定伺服器上的資料庫目錄。Specifies database catalog on server.
/user: <名稱>/user: <name> 指定登入使用者識別碼。預設值:使用 Windows 驗證。Specifies logon user id. Default value: Use Windows authentication.
/password: <密碼>/password: <password> 指定登入密碼。Specifies logon password. 預設值:使用 Windows 驗證。Default value: Use Windows authentication.
/conn: <連接字串>/conn: <connection string> 指定資料庫連接字串。Specifies database connection string. 不可配合 /server/database/user/password 選項使用。Cannot be used with /server, /database, /user, or /password options.

不要在連接字串中包含檔案名稱。Do not include the file name in the connection string. 而是在命令列中加入檔名來做為輸入檔案。Instead, add the file name to the command line as the input file. 例如,下行指定 "c:\northwnd.mdf" 作為輸入檔案: sqlmetal /code:"c:\northwind.cs" /language:csharp "c:\northwnd.mdf"For example, the following line specifies "c:\northwnd.mdf" as the input file: sqlmetal /code:"c:\northwind.cs" /language:csharp "c:\northwnd.mdf".
/timeout: <秒>/timeout: <seconds> 指定 SqlMetal 存取資料庫時的逾時值。Specifies time-out value when SqlMetal accesses the database. 預設值:0 (也就是沒有時間限制)。Default value: 0 (that is, no time limit).

擷取選項Extraction options

選項Option 說明Description
/views/views 擷取資料庫檢視。Extracts database views.
/functions/functions 擷取資料庫函式。Extracts database functions.
/sprocs/sprocs 擷取預存程序。Extracts stored procedures.

輸出選項Output options

選項Option 說明Description
/dbml [:file]/dbml [:file] 以 .dbml 傳送輸出。Sends output as .dbml. 無法搭配 /map 選項使用。Cannot be used with /map option.
/code [:file]/code [:file] 以原始程式碼傳送輸出。Sends output as source code. 無法搭配 /dbml 選項使用。Cannot be used with /dbml option.
/map [:file]/map [:file] 產生 XML 對應檔而不是屬性。Generates an XML mapping file instead of attributes. 無法搭配 /dbml 選項使用。Cannot be used with /dbml option.

其他Miscellaneous

選項Option 說明Description
/language: <語言>/language: <language> 指定原始程式碼語言。Specifies source code language.

有效的 <語言>:vb、csharp。Valid <language>: vb, csharp.

預設值:衍生自程式碼檔案名稱的副檔名。Default value: Derived from extension on code file name.
/namespace: <名稱>/namespace: <name> 指定所產生程式碼的命名空間。Specifies namespace of the generated code. 預設值:沒有命名空間。Default value: no namespace.
/context: <類型>/context: <type> 指定資料庫內容類別的名稱。Specifies name of data context class. 預設值:衍生自資料庫名稱。Default value: Derived from database name.
/entitybase: <類型>/entitybase: <type> 指定所產生程式碼中實體類別的基底類別。Specifies the base class of the entity classes in the generated code. 預設值:實體沒有基底類別。Default value: Entities have no base class.
/pluralize/pluralize 自動複數化或單數化類別和成員名稱。Automatically pluralizes or singularizes class and member names.

這個選項功能僅適用於美國英文版本。This option is available only in the U.S. English version.
/serialization: <選項>/serialization: <option> 產生可序列化的類別。Generates serializable classes.

有效的 <選項 >:無、單向。Valid <option>: None, Unidirectional. 預設值:無。Default value: None.

如需詳細資訊,請參閱序列化For more information, see Serialization.

輸入檔案Input File

選項Option 說明Description
<輸入檔><input file> 指定 SQL Server Express .mdf 檔、 SQL Server Compact 3.5SQL Server Compact 3.5 .sdf 檔或是 .dbml 中繼檔。Specifies a SQL Server Express .mdf file, a SQL Server Compact 3.5SQL Server Compact 3.5 .sdf file, or a .dbml intermediate file.

備註Remarks

SqlMetal 功能實際上包含兩個步驟:SqlMetal functionality actually involves two steps:

  • 將資料庫的中繼資料擷取至 .dbml 檔。Extracting the metadata of the database into a .dbml file.

  • 產生程式碼輸出檔。Generating a code output file.

    使用適當的命令列選項,您就可以產生 Visual Basic 或 C# 原始程式碼,或是產生 XML 對應檔。By using the appropriate command-line options, you can produce Visual Basic or C# source code, or you can produce an XML mapping file.

若要從 .mdf 檔擷取中繼資料,您必須在所有其他選項之後指定 .mdf 檔的名稱。To extract the metadata from an .mdf file, you must specify the name of the .mdf file after all other options.

如果沒有指定 /server ,則會假設為 localhost/sqlexpressIf no /server is specified, localhost/sqlexpress is assumed.

如果下列其中一個或多個條件為真,則Microsoft SQL Server 2005Microsoft SQL Server 2005 會擲回例外狀況:Microsoft SQL Server 2005Microsoft SQL Server 2005 throws an exception if one or more of the following conditions are true:

  • SqlMetal 嘗試擷取呼叫本身的預存程序。SqlMetal tries to extract a stored procedure that calls itself.

  • 預存程序、函式或檢視的巢狀層超過 32。The nesting level of a stored procedure, function, or view exceeds 32.

    SqlMetal 會快取這個例外狀況並回報為警告。SqlMetal catches this exception and reports it as a warning.

若要指定輸入檔案名稱,請將名稱以輸入檔案加入命令列。To specify an input file name, add the name to the command line as the input file. 不支援將檔案名稱包含在連接字串中 (使用 /conn 選項)。Including the file name in the connection string (using the /conn option) is not supported.

範例Examples

產生 .dbml 檔,其中包含擷取的 SQL 中繼資料:Generate a .dbml file that includes extracted SQL metadata:

sqlmetal /server:myserver /database:northwind /dbml:mymeta.dbmlsqlmetal /server:myserver /database:northwind /dbml:mymeta.dbml

產生 .dbml 檔,其中包括使用 SQL Server Express 從 .mdf 檔擷取的 SQL 中繼資料:Generate a .dbml file that includes extracted SQL metadata from an .mdf file by using SQL Server Express:

sqlmetal /dbml:mymeta.dbml mydbfile.mdfsqlmetal /dbml:mymeta.dbml mydbfile.mdf

產生 .dbml 檔,其中包括從 SQL Server Express 擷取的 SQL 中繼資料:Generate a .dbml file that includes extracted SQL metadata from SQL Server Express:

sqlmetal /server:.\sqlexpress /dbml:mymeta.dbml /database:northwindsqlmetal /server:.\sqlexpress /dbml:mymeta.dbml /database:northwind

從 .dbml 中繼資料檔產生原始程式碼:Generate source code from a .dbml metadata file:

sqlmetal /namespace:nwind /code:nwind.cs /language:csharp mymetal.dbmlsqlmetal /namespace:nwind /code:nwind.cs /language:csharp mymetal.dbml

直接從 SQL 中繼資料產生原始程式碼:Generate source code from SQL metadata directly:

sqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharpsqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharp

注意

當您使用 /pluralize 選項搭配 Northwind 範例資料庫時,請注意以下行為:When you use the /pluralize option with the Northwind sample database, note the following behavior. 當 SqlMetal 提供資料表的資料列類型名稱時,資料表名稱會是單數。When SqlMetal makes row-type names for tables, the table names are singular. 當它為資料表提供 DataContext 屬性時,資料表名稱會是複數。When it makes DataContext properties for tables, the table names are plural. 碰巧的是,Northwind 範例資料庫中的資料表已經是複數。Coincidentally, the tables in the Northwind sample database are already plural. 因此您不會看見該部分的運作情形。Therefore, you do not see that part working. 雖然一般會將資料庫資料表的名稱設為單數,在 .NET 中仍然常會把集合名稱設為複數。Although it is common practice to name database tables singular, it is also a common practice in .NET to name collections plural.

另請參閱See also