程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C# 3.0入門系列(七)--之OR工具介紹(上)

C# 3.0入門系列(七)--之OR工具介紹(上)

編輯:關於C#

……不得不再次給關注dlinq的朋友道歉了。好久都沒有更新blog了。因為工作的變動,還要趕期限,沒有時間關注這裡了。

先發布一則消息。Orcas Beta1, 這個才是beta1,可以到http://www.microsoft.com/downloads/details.aspx?FamilyID=f10fb5df-e369-4db4-b9a7-845dbf793368&DisplayLang=en 下載。5月1號的版本。最早4月19號就出來過一個,只是沒有在意。還有一個http://www.microsoft.com/downloads/details.aspx?FamilyID=36b6609e-6f3d-40f4-8c7d-ad111679d8dc&DisplayLang=en 。 一個是self-extracting 版本的,一個是Virtual PC version的。不知道什麼區別。沒有裝過。

本節捨去原來的計劃,而改講映射工具。在入門三一文中,我們提到了sqlmetal這個工具。http://www.cnblogs.com/126/archive/2006/09/06/492332.html sqlmetal的功能是將數據庫的信息抽提出來,生成映射代碼。Orcas還有另外一個工具,就是O/R Designer.

先講sqlmetal. 自上次的版本後,sqlmetal,又增加了一些新的功能。比如,支持SQLCE版本的Sql Server, 支持直接輸入connection string, 等等。 這是sqlmetal的幫助信息,是不是比上次多了很多?

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表拖入 如圖所示:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved