先讲sqlmetal. 自上次的版本后,sqlmetal,又增加了一些新的功能。比如,支持SQLCE版本的Sql Server, 支持直接输入connection string, 等等。 这是sqlmetal的帮助信息,是不是比上次多了很多?
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
Microsoft (R) Database Mapping Generator 2008 Beta 2 version 1.00.20612
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
SqlMetal [options] [<input file>]
Generates code and mapping for the LINQ to SQL component of the .NET framework. SqlMetal can:
- Generate source code and mapping attributes or a mapping file from a database.
- Generate an intermediate dbml file for customization from the database.
- Generate code and mapping attributes or mapping file from a dbml file.
Options:
/server:<name> Database server name.
/database:<name> Database catalog on server.
/user:<name> Login user ID (default: use Windows Authentication).
/password:<password> Login password (default: use Windows Authentication).
/conn:<connection string> Database connection string. Cannot be used with /server, /database, /us
er or /password options.
/timeout:<seconds> Timeout value to use when SqlMetal accesses the database (default: 0 wh
ich means infinite).
/views Extract database views.
/functions Extract database functions.
/sprocs Extract stored procedures.
/dbml[:file] Output as dbml. Cannot be used with /map option.
/code[:file] Output as source code. Cannot be used with /dbml option.
/map[:file] Generate mapping file, not attributes. Cannot be used with /dbml option
.
/language:<language> Language for source code: VB or C# (default: derived from extension on code file name).
/namespace:<name> Namespace of generated code (default: no namespace).
/context:<type> Name of data context class (default: derived from database name).
/entitybase:<type> Base class of entity classes in the generated code (default: entities have no base class).
/pluralize Automatically pluralize or singularize class and member names using English language rules.
/serialization:<option> Generate serializable classes: None or Unidirectional (default: None).
/provider:<type> Provider type (default: provider is determined at run time).
<input file> May be a SqlExpress mdf file, a SqlCE sdf file, or a dbml intermediate file.
Create code from SqlServer:
SqlMetal /server:myserver /database:northwind /code:nwind.cs /namespace:nwind
Generate intermediate dbml file from SqlServer:
SqlMetal /server:myserver /database:northwind /dbml:northwind.dbml /namespace:nwind
Generate code with external mapping from dbml:
SqlMetal /code:nwind.cs /map:nwind.map northwind.dbml
Generate dbml from a SqlCE sdf file:
SqlMetal /dbml:northwind.dbml northwind.sdf
Generate dbml from SqlExpress local server:
SqlMetal /server:.\sqlexpress /database:northwind /dbml:northwind.dbml
Generate dbml by using a connection string in the command line:
SqlMetal /conn:"server='myserver'; database='northwind'" /dbml:northwind.dbml
因为sqlmetal 已经讲过,不再多讲,大家知道它是个命令行的工具,用来做映射数据库信息的,会使用就可以。 再接着讲O/R Designer. 新建任一工程。右击工程,选择add->new item. 如图所示:
弹出item对话框,category中选择data,然后选择Linq to Sql Class, 如图所示:
选择添加。这样,你的工程就多了一个O/R Desiger项目。在菜单View->Sever Explore 打开Sever Explore,添加一个Data Connection, 如图所示:
后,填写数据库服务器,用户名,密码和数据库名。如果,你没有,你可以使用Sql Express 版本。添加之后的效果,如图所示:
打开刚才添加的DataClasses1.dbml 文件,将Sever Explore中的Customer 表或view拖入Designer中来,如图所示:
也可以将Store procedure 和 User define function 拖进来,会在方法面板上形成方法。如图所示:
如果,两个表之间有关系,OR Designer可以显示它们的关系,将Order表和 OderDetail表拖入 如图所示:
打开DataClasses1. Designer.cs 文件,你就可以看到OR Designer给你产生的映射代码了。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12641349/viewspace-144774/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12641349/viewspace-144774/