SubSnoic 框架入門到提高(1)---全程記錄


關於這個框架的介紹我就不說了,網上很多,我只教基本用法,基本邏輯語句寫法

以sqlserver2008+SubSonic 2.2+VS2010

 

為了讓學習者能順利學習,我已將subsonic2.2上傳到了csdn上了 ,下載地址:http://download.csdn.net/download/yangyanghaoran/4318138

為了防止惡意轉載:本文地址 http://www.cnblogs.com/Fresh-Air/archive/2012/05/21/2511578.html

一:現在D盤建一個文件夾:SubsonicTest, 在該目錄下放置你需要的文件 SubSonic2.2.ZIP 文件解壓了,放在了這里

   

二:打開sqlserver2008,執行下面的腳本,創建我們需要的數據庫

use master
go
--創建庫
if exists(select * from sysdatabases where name='SubSonicTestDB')
drop database SubSonicTestDB
create database SubSonicTestDB
on primary
(
name='SubSonicTestDB_data',
filename='D:\SubsonicTest\SubSonicTestDB_data.mdf',
filegrowth=30%,
size=5
)log on
(
name='SubSonicTestDB_log',
filename='D:\SubsonicTest\SubSonicTestDB_log.ldf',
size=2,
filegrowth=10%
)
go

--創建表Student
use SubSonicTestDB
go
if exists(select * from sysobjects where name='Teacher')
drop table Teacher
create table Teacher(
TeacherID int identity(1,1) primary key,
TeacherName varchar(20),
TeacherPhone varchar(13)
constraint VK_TeacherPhone check(TeacherPhone like '____-________' or TeacherPhone like '___-________' or TeacherPhone like'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
)

if exists(select * from sysobjects where name='Student')
drop table Student
create table Student(
StudentID int identity(1,1) primary key,
StudentName varchar(20),
StudentHobby varchar(20),
TeacherID int
constraint FK_TeacherID foreign key(TeacherID) REFERENCES Teacher(TeacherID)
)
go
insert into Teacher(TeacherName,TeacherPhone)
select '張老師','15252025205' union all
select '劉老師','15252025202' union all
select '田老師','15252025235' union all
select '盧老師','15252025234' union all
select '王老師','15252025206'
go
insert into Student(StudentName,StudentHobby,TeacherID)
select '小慈','打羽毛球',1 union all
select '小明','下圍棋,讀英語',1 union all
select '小方','看漫畫',2 union all
select '小龍','武術',3 union all
select '小虎','中國象棋',5 union all
select '小城','聽聽音樂',1 union all
select '小美','說英語,日語',4 union all
select '小聰','看漫畫,踢毽子',2 union all
select '小紅','看愛情電影',3 union all
select '小仲','中國象棋',5 union all
select '小冬','學習編程',3 
go

 

三、打開VS2010 

  1.新建解決方案  名稱:SubSonicTest,放在D盤的那個文件夾下

 

2.在該解決方案下新建類庫:SubSonicDAL 

    ① 刪除默認生成的class1.cs文件

    ②引用SubSonic.dll(前提是先安裝了subsonic才會有這個)

       System.Web和System.Configuration命名空間

   

    ③添加配置文件 App.config

   

下面是我app.config文件的內容,這是搜吧subsonic的基本配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <!-- 此處不要隨便修改 -->
    <configSections>
      <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/>
    </configSections>

    <!-- 這里定義一個或者多個連接數據庫的字符串,其中要注意的是這部分的name的值是下面connectionStringName屬性的值-->
    <connectionStrings>
      <!-- 連接數據庫的字符串 -->
      <add name="StudentConn"
           connectionString="Data Source=.;Initial Catalog=SubSonicTestDB;Integrated Security=True"/>
    </connectionStrings>

    <!-- 此處是將連接字符串和數據庫驅動匹配起來成為一個連接,name是連接的名字,generatedNamespace的值是生成對象的命名空間-->
    <SubSonicService defaultProvider="StudentConn">
      <providers>
        <clear/>
        <add name="StudentConn"
             type="SubSonic.SqlDataProvider, SubSonic"
             connectionStringName="StudentConn"
             generatedNamespace="StudentMGR"/>
      </providers>
    </SubSonicService>

</configuration>

3.配置工具

          在vs中工具菜單中選擇 “工具” -> “外部工具” 命令,定義一個外部工具菜單項,

          點添加創建一個新的工具

           標題為SubSonic Tools(也可以自己命名),

           命令為SubSonic文件夾的中命令行工具sonic.exe的路徑,

           參數為:generate /out Generated(生成后的路徑為當前路徑下的Generated文件夾。),

           初始目錄為:$(ProjectDir),

           並勾選“使用命令窗口”和“提示輸入參數”兩個選項,點確定。

         

然后點確定。接下來就是見證奇跡的時刻了

此時vs的工具菜單多了一項“SubSonic DAL命令,單擊,然后確定。

執行完畢,在Generated文件夾中會生成你設定的數據庫的類庫文件,包括表、視圖、存儲過程的c#包裝。如果出現錯誤,一般是因為數據庫連接串有問題,請仔細檢查

然后你的解決方案應該會這樣:

 

包含到項目中,然后展開如圖,然后編譯,生成類庫

OK,所有工作基本完成了,接下來該學習怎么用這些生成好的文件了

關於更刪改查,分頁在下一篇博客寫,如有疑問請留言

 

 

 

           

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM