Code Generation Tool (sqlmetal.exe)

Source: Internet
Author: User
Tags microsoft sql server 2005 sql server express

The sqlmetal command line tool can be generated for the. NET Framework's LINQ to SQL component.CodeAnd ing. By applying the options next to this topic, you can instruct sqlmetal to perform several different operations, including:

    • Generate from databaseSource codeAnd ing properties or ing files.

    • Generate an intermediate database Markup Language (. dbml) file for custom use from the database.

    • Generate code and ing attributes or ing files from the. dbml file.

By default, the sqlmetal file is locatedDrive: \ Program Files \ microsoft sdks \ windows \ vN. nn\ Bin.

Note:

Visual Studio developers can also use object link designer to generate object classes. For large databases, this command line method has good scalability. Because sqlmetal is a command line tool, it can be used during the generation process.For more information, see object link designer (O/R designer ).

Sqlmetal [Options] [<Input File>]
Option

To view the latest Option List, enterSqlmetal /?.

Connection options

Option

Description

/Server: <Name>

Specify the name of the database server.

/Database: <Name>

Specifies the database directory on the server.

/User: <Name>

Specify the logon user ID. Default Value: Windows authentication.

/Password: <Password>

Specify the logon password. Default Value: Windows authentication.

/Conn: <Connection string>

Specifies the database connection string. Cannot match/Server,/Database,/UserOr/PasswordOption.

Do not include the file name in the connection string. Instead, the file name should be added to the command line as the input file. For example, the following command specifies "C: \ northwnd. MDF" as the input file:Sqlmetal/Code: "C: \ northwind. cs"/language: CSHARP "C: \ northwnd. MDF".

/Timeout: <Seconds>

Specifies the timeout value when sqlmetal accesses the database. Default Value: 0 (no time limit ).

Extraction options

Option

Description

/Views

Extract the database view.

/Functions

Extract database functions.

/Sprocs

Extract the stored procedure.

Output options

Option

Description

/Dbml [: File]

Send the output with the. dbml extension. Cannot match/MapOption.

/Code [: File]

Send the output as source code. Cannot match/DbmlOption.

/Map [: File]

Generate an XML ing file instead of an attribute. Cannot match/DbmlOption.

Miscellaneous

Option

Description

/Language: <Language>

Specifies the source code language.

Valid<Language>Including: VB and CSHARP.

Default Value: Derived from the code file extension.

/Namespace: <Name>

Specify a namespace for the generated code. Default Value: No namespace.

/Context: <Type>

Specifies the name of the data context class. Default Value: Derived from the database name.

/Entitybase: <Type>

Specify the base class for the entity class in the generated code. Default Value: the object does not have a base class.

/Pluralize

Automatically applies the plural or singular form to the class and member names.

This option is only available in English (us.

/Serialization: <Option>

Generate serializable classes.

Valid<Option>Including none and unidirectional. Default Value: none.

For more information, see serialization (LINQ to SQL ).

Input File

Option

Description

<Input File>

Specify the SQL Server Express. MDF file, SQL Server compact 3.5. SDF file, or. dbml intermediate file.

Remarks

The sqlmetal function involves two steps:

    • Extract the metadata of the database to A. dbml file.

    • Generate a code output file.

      You can use appropriate command line options to generate visual basic or C # source code, or an XML ing file.

To extract metadata from the. MDF file, you must specify the name of the. MDF file after all other options.

If not specified/ServerIs assumed to beLocalhost/sqlexpress.

If one or more of the following conditions exist, Microsoft SQL Server 2005 will cause an exception:

    • Sqlmetal tries to extract the Stored Procedure for self-calling.

    • The nesting level of stored procedures, functions, or views exceeds 32.

      Sqlmetal will capture this exception and report it as a warning.

To specify an input file name, add the name as the input file to the command line. The file name cannot be included in the connection string./ConnOption ).

Example

Generate a. dbml file containing the extracted SQL metadata:

Sqlmetal/Server: myserver/Database: northwind/dbml: mymeta. dbml

Use SQL Server express to generate a. dbml file that contains the SQL metadata extracted from the. MDF file:

S qlmetal/dbml: mymeta. dbml mydbfile. MDF

Generate a. dbml file that contains SQL metadata extracted from SQL Server Express:

Sqlmetal/Server:. \ sqlexpress/dbml: mymeta. dbml/Database: northwind

Source code generated based on the. dbml metadata file:

Sqlmetal/namespace: nwind/code: nwind. CS/language: CSHARP mymetal. dbml

Generate Source Code directly based on SQL metadata:

Sqlmetal/Server: myserver/Database: northwind/namespace: nwind/code: nwind. CS/language: CSHARP

Note:

If you apply/PluralizeOption, pay attention to the following behavior. If sqlmetal creates a row name for the table, the table name will be in the singular format. If the datacontext attribute is created for the table, the table name is in the plural form. Coincidentally, the table name in the northwind sample database is already in the plural form. Therefore, you will not see this effect. Although database tables are generally named in the singular form, it is also common to name a set in. NET as a plural form.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.