LinqToSql--DataContext与实体
DataContext类型是System.Data.Linq命名空间下的重要类型,用于把查询语法翻译成Sql语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库.
DataContext提供了以下常用功能:
1.以日志形式记录DataContext生成的Sql
2.执行Sql
3.创建和删除数据库
DataContext是实体和数据库之间的桥梁,那么我们首先需要定义映射到数据库的实体
定义实体类:
namespace DE { [Table(Name="users")] public class Users { public Users() { // //TODO: 在此处添加构造函数逻辑 // } [Column(Name="userid",IsPrimaryKey=true,DbType="int")] public int UserId { get; set; } [Column(Name="username")] public string UserName { get; set; } [Column(Name="userpwd")] public string UserPwd { get; set; } [Column(Name="age")] public int Age { get; set; } } }
其中UserId字段是主键,如果没有指定属性中的Column特性的Name属性,那么系统会把属性名作为数据库的字段名也就是说实体类的属性名就需要和数据库表中的字段名一致.
页面调用1:
创建一个Asp.Net页面,加入一个GridView,后台代码如下:
protected void Page_Load(object sender, EventArgs e) { string connstr = ConfigurationManager.ConnectionStrings["ReportConnectionString"].ToString(); DataContext dc = new DataContext(connstr); GridView1.DataSource = dc.GetTable<DE.Users>(); GridView1.DataBind(); }
页面效果如下:
使用强类型的DataContext
public class UsersDataContext : DataContext { public Table<DE.Users> Users; public UsersDataContext(IDbConnection connection) : base(connection) { } public UsersDataContext(string connection) : base(connection) { } }
此时,页面调用如下:
protected void Page_Load(object sender, EventArgs e) { string connstr = ConfigurationManager.ConnectionStrings["ReportConnectionString"].ToString(); UsersDataContext dc = new UsersDataContext(connstr); GridView1.DataSource = dc.Users; GridView1.DataBind(); }
DataContext的日志功能
protected void Page_Load(object sender, EventArgs e) { string connstr = ConfigurationManager.ConnectionStrings["ReportConnectionString"].ToString(); UsersDataContext dc = new UsersDataContext(connstr); System.IO.TextWriter tw = new System.IO.StreamWriter(Server.MapPath("log.txt"),true); dc.Log = tw; GridView1.DataSource = dc.Users; GridView1.DataBind(); tw.Close(); }
此时注意将log.txt设置为everyone属性.执行页面,内容如下:
SELECT [t0].[userid] AS [UserId], [t0].[username] AS [UserName], [t0].[userpwd] AS [UserPwd], [t0].[age] AS [Age] FROM [users] AS [t0] -- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 3.5.30729.1
执行Sql语句
protected void Page_Load(object sender, EventArgs e) { string connstr = ConfigurationManager.ConnectionStrings["ReportConnectionString"].ToString(); DataContext dc = new DataContext(connstr); string UpdateSql = "@userid"; System.IO.TextWriter tw = new System.IO.StreamWriter(Server.MapPath("log.txt"),true); dc.Log = tw; dc.ExecuteCommand("update users set age=10 where userid={0}",16); GridView1.DataSource = dc.ExecuteQuery<DE.Users>("select * from users"); GridView1.DataBind(); tw.Close(); }
页面效果:
查询日志,结果如下:
update users set age=10 where userid=@p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [16]
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 3.5.30729.1
select * from users
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 3.5.30729.1
创建数据库
步骤:
1.建立映射类
[Table(Name="testTable")] public class testTable { [Column(Name="sid",DbType="int")] public int sid { get; set; } [Column(Name="Name",DbType="nvarchar(20)")] public string Name { get; set; } }
2.建立DataContext实体类
public class DefContext : DataContext { public DefContext(IDbConnection conn) : base(conn) { } public Table<testTable> testTable; }
3.前台代码如下:
protected void Page_Load(object sender, EventArgs e) { string connst = "server=.;database=re;uid=sa"; IDbConnection conn = new SqlConnection(connst); DefContext dc = new DefContext(conn); dc.CreateDatabase(); }
4.结果
建立了一个名为re的数据库.在re数据库中建立了一个表,即为上面定义的表
注意:
无法建立一个空的数据库,要建立数据库的同时,相应的也必须在该库中建立一个表.