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,所有工作基本完成了,接下来该学习怎么用这些生成好的文件了

关于更删改查,分页在下一篇博客写,如有疑问请留言

 

           

    

原文地址:https://www.cnblogs.com/AaronYang/p/2511578.html