LINQ To SQLTest
来源:互联网 发布:ios 网络加载动画 编辑:程序博客网 时间:2024/04/30 19:03
一. 创建一个Web项目,添加一个实体类User,供LINQ创建Table,代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.Linq;
- using System.Data.Linq.Mapping;
- namespace LinqToSQL
- {
- /// <summary>
- /// 创建实体类
- /// </summary>
- [Table(Name ="tb_User")]
- public class User
- {
- [Column(IsPrimaryKey = true)]
- public int ID { get; set; }
- [Column(Name = "UserName")]
- public string UserName { get; set; }
- [Column(Name = "Address")]
- public string Address { get; set; }
- [Column(Name = "Sex")]
- public string Sex { get; set; }
- [Column(Name = "Email")]
- public string Email { get; set; }
- [Column(Name = "Phone")]
- public string Phone { get; set; }
- }
- }
第二,如果使用强类型DataContext则需要加一个强类型的UserDataContext类,代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data ;
- using System.Data .Linq ;
- namespace LinqToSQL
- {
- /// <summary>
- /// User表的强类型的DataContext类型
- /// </summary>
- public class UserDataContext:DataContext
- {
- public Table<User> tableUser;
- public UserDataContext(IDbConnection connection) : base(connection) { }
- public UserDataContext(string connection) : base(connection) { }
- }
- }
第三, 在页面Default.aspx页面上添加一个GridView控件,GridView控件,使用下面的代码进行绑定数据,后台书写后台代码如下:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.Linq;
- using LinqToSQL;
- using System.IO;
- using System.Data.Common;
- using System.Data.SqlClient;
- namespace LinqSQLWeb
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- //使用DataContext
- const string connStr = "Data Source=(local);User Id=sa;Password=server;Initial Catalog=db_Test;";
- //创建一个DataContext对象
- DataContext db = new DataContext(connStr);
- //创建一个Table<>类
- Table<User> tableUser = db.GetTable<User>();
- //用LINQ查询来显示所有的数据
- var subset1 = from u in tableUser where u.Sex == "女" orderby u.UserName select u;
- GridView1.DataSource = subset1;
- GridView1.DataBind();
- //使用强类型的DataContext
- UserDataContext udataContext = new UserDataContext(connStr);
- //记录日志
- StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
- udataContext.Log = sw;
- var subset2 = from u in udataContext.tableUser where u.Sex == "女" orderby u.UserName select u;
- DbCommand dbCommand = udataContext.GetCommand(subset2);
- Response.Write(dbCommand.CommandText );
- foreach (DbParameter parm in dbCommand.Parameters)
- {
- Response.Write(string.Format("<br />参数名:{0},参数值:{1}<br/>", parm.ParameterName, parm.Value));
- }
- GridView2.DataSource = subset2;
- GridView2.DataBind();
- //LINQ使用DbDataReader数据源
- var conn = new SqlConnection(connStr);
- var ctx = new DataContext(conn);
- var cmd = new SqlCommand("select * from tb_User", conn);
- conn.Open();
- var reader = cmd.ExecuteReader();
- GridView3.DataSource = ctx.Translate<User>(reader);
- GridView3.DataBind();
- sw.Close();
- }
- }
- }
- }
运行程序后在网站所在目录生成了
log.txt,每次查询都会把诸如下面的日志追加到文本文件中:SELECT [t0].[CustomerID], [t0].[ContactName], [t0].[City] FROM [Customers] AS [t0] WHERE [t0].[CustomerID] LIKE @p0 -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1 应该说这样的日志对于调试程序是非常有帮助的。
- LINQ To SQLTest
- linq,linq to sql
- SqlTest(2013-07-10)
- linq学习 Linq to DataSet
- Linq初探--Linq to SQL
- LINQ(LINQ to Entities)
- LINQ解析:LINQ to Objects
- LINQ(LINQ to Entities)
- LINQ(LINQ to Entities)
- LINQ(LINQ to DataSet)
- LINQ to Objects 学习
- LINQ to SQL 学习
- 什么是Linq to sql
- 使用LINQ to XML
- LINQ TO XML
- 使用LINQ to SQL
- LINQ TO XML学习
- Linq to Objects
- 鼠标右键.单击判断.选中行后并弹出右键菜单.
- JasperReports开发指南
- 通过金矿模型介绍动态规划
- configure glib
- 正则表达式的学习网站
- LINQ To SQLTest
- 介绍
- VC下利用MSMAPI32.OCX编写邮件功能
- [双语阅读]韩国研发出会跳舞的机器人
- 疲劳之中寻找本我
- [双语阅读]揭秘:巴赫部分名曲为其妻子所作
- 推荐两个不错的软件
- 支持暂停和继续操作的WAV音频播放
- 十七届三中“全会”