using System;
using System.Linq;
using System.Data.Linq;
namespace 简单的ORM和LINQToSQL示例
{
class Program
{
//定义一个私有的连接字符串
private static readonly string connectionString = "Data Source=.;Initial Catalog=db_Customer;Integrated Security=SSPI;";
static void Main(string[] args)
{
//表示Linq to SQL框架的主入口点,DataContext实例,通过一个连接字符串连接你的数据库
DataContext customerContext = new DataContext(connectionString);
//Table:表示基础数据库中特定类型的表。
//GetTable:返回特定类型的对象的集合,其中类型由 TEntity 参数定义
Table customers = customerContext.GetTable();
var seperator = new string('*', 40);
Console.WriteLine(seperator);
//查询所有数据
var startsWith = from c in customers
select c;
Array.ForEach(startsWith.ToArray(), c => Console.WriteLine(c));
//Log:获取或设置为目标编写SQL查询或命令
//Out:获取标准输出流
//customerContext.Log = Console.Out;
Console.WriteLine(seperator);
//查询单条数据(按条件查询)
var singleInfo = from c in customers
where c.CustomerID[c.CustomerID.Length - 1] == '3'
select c;
Array.ForEach(singleInfo.ToArray(), c => Console.WriteLine(c));
Console.WriteLine(seperator);
//删除指定的一条数据,根据字段CustomerID='1009'
var deleteInfo = customers.SingleOrDefault(s => s.CustomerID == "1009");
//Console.WriteLine(deleteInfo);
if (deleteInfo == null)
{
throw new Exception ("没有该条数据");
}
customers.DeleteOnSubmit(deleteInfo);
customerContext.SubmitChanges();
Console.ReadKey();
}
}
}
using System;
using System.Linq;
using System.Data.Linq;
using 简单的ORM和LINQToSQL示例;
using System.Collections.Generic;
namespace 增加修改记录
{
class Program
{
private static readonly string connectionString = "Data Source=.;Initial Catalog=db_Customer;Integrated Security=SSPI;";
static void Main(string[] args)
{
DataContext context = new DataContext(connectionString);
Table customers = context.GetTable();
var seperator = new string('*', 40);
//插入一条记录
Customer c = new Customer
{
CustomerID = "201",
ContactName = "徐帅",
Phone = "12122121121",
CompanyName="妈蛋的哈哈哈"
};
customers.InsertOnSubmit(c);
//插入多条数据
var cc = new List
{
new Customer {CustomerID="202",ContactName="大胖", Phone="32323232323",CompanyName="公司名" },
new Customer {CustomerID="003",ContactName="fsfds", Phone="656765768",CompanyName="公司的名字" },
new Customer {CustomerID="004",ContactName="艰苦艰苦", Phone="0897876565",CompanyName="公司的haoming" }
};
customers.InsertAllOnSubmit(cc);
//修改一条记录
var editInfo = customers.SingleOrDefault(s => s.CustomerID =="1001");
editInfo.ContactName = "修改秀嘎";
context.SubmitChanges();
Console.ReadKey();
}
}
}
[Column(IsPrimaryKey =true,Name = "CustomerID",Storage ="customerid",CanBeNull =false,DbType ="varchar(20)")]
public string CustomerID { get; set; }
三、查看由LINQ生成的查询文本
/表示Linq to SQL框架的主入口点,DataContext实例,通过一个连接字符串连接你的数据库
DataContext customerContext = new DataContext(connectionString);
//Table:表示基础数据库中特定类型的表。
//GetTable:返回特定类型的对象的集合,其中类型由 TEntity 参数定义
Table customers = customerContext.GetTable();
Console.WriteLine("SELECT:{0}", customerContext.GetCommand(customers).CommandText);
SELECT:SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
FROM [tb_Customer] AS [t0]
using System;
using System.Linq;
using 查看由LINQ生成的查询文本;
using System.Data.Linq;
namespace 通过DataContext对象联接关系型数据
{
class Program
{
static void Main(string[] args)
{
newDataContext context = new newDataContext();
Table details = context.GetTable();
//查看SQL文本
Console.WriteLine("Select:{0}", context.GetCommand(details).CommandText);
Console.WriteLine();
var result = from detail in details
where detail.OrderID == "1005"
select detail;
Array.ForEach(result.ToArray(), r => Console.WriteLine(r));
Console.ReadKey();
}
}
public class newDataContext : DataContext
{
private static readonly string connectionString = "Data Source=.;Initial Catalog=db_Customer;Integrated Security=true";
public newDataContext() : base(connectionString)
{
}
}
}
类名 | 扩展方法 | 备注 |
DataRowExtensions | Field SetField | 行扩展方法提供对数据的字段级访问 |
DataTableExtensions | AsDataView AsEnumerable CopyToDataTable | 表扩展方法将会产生一个可查询的序列 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
namespace 从DataTable中获取数据
{
class Program
{
private static readonly string connectionString = "Data Source=.;Initial Catalog=db_Customer;Integrated Security=true";
static void Main(string[] args)
{
//创建一个存储数据的容器“内存”
DataSet data = new DataSet();
using(SqlConnection con=new SqlConnection(connectionString))
{
con.Open();
string selectSQL = "select * from tb_OrderDetail";
SqlCommand com = new SqlCommand(selectSQL, con);
//查询数据
SqlDataAdapter adapter = new SqlDataAdapter(com);
//将查询到的数据以表的形式放到data中
adapter.Fill(data, "OrderDetails");
}
//定义一个数据表并找到容器中的表
DataTable orderDetailTable = data.Tables["OrderDetails"];
//对该表进行查询、筛选、排序 ,使用了ADO.NET和一个LINQ to DataSet查询
//而这个查询则使用了AsEnumerable扩展方法
IEnumerable orderDetails = from orderDetail in orderDetailTable.AsEnumerable()
where orderDetail.Field("discount")> 0.8
orderby orderDetail.Field("discount")
select orderDetail;
Console.WriteLine("OrderDetai Information :");
foreach (DataRow row in orderDetails.Take(3))
{
Console.WriteLine("订单号:"+row.Field("orderid"));
Console.WriteLine("产品编号:"+row.Field("productid"));
Console.WriteLine("价格:"+row.Field("unitprice"));
Console.WriteLine("销售总数:"+row.Field("quantity"));
Console.WriteLine("折扣率:"+row.Field("discount")==null?0: row.Field("discount"));
Console.WriteLine(Environment.NewLine);
}
Console.ReadKey();
}
}
}
using System;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
namespace 在DataSet上定义联接
{
class Program
{
private static readonly string connectionString = "Data Source=.;Initial Catalog=db_Customer;Integrated Security=true";
static void Main(string[] args)
{
//一次性调用多个SQL语句
const string selectSQL = "select * from tb_OrderDetail;select * from tb_Order;";
DataSet data = new DataSet();
using(SqlConnection con=new SqlConnection(connectionString))
{
con.Open();
SqlCommand com = new SqlCommand(selectSQL, con);
com.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(com);
adapter.Fill(data);
}
DataTable orders = data.Tables[0];
DataTable orderDetails = data.Tables[1];
var orderResults = from order in orders.AsEnumerable()
join detail in orderDetails.AsEnumerable()
on order.Field("orderid") equals detail.Field("orderid")
orderby order.Field("discount")
select new
{
OrderID = detail.Field("orderid"),
ProductID = detail.Field("productid"),
UnitPrice = order.Field("unitprice"),
Quantity = order.Field("quantity"),
Discount = order.Field("discount")
};
Console.WriteLine("Orders & Details");
foreach (var item in orderResults)
{
Console.WriteLine("Order ID:{0}", item.OrderID);
Console.WriteLine("Product ID:{0}", item.ProductID);
Console.WriteLine("Unitprice:{0}", item.UnitPrice);
Console.WriteLine("Quantity:{0}", item.Quantity);
Console.WriteLine("Discount:{0}", item.Discount);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
实现方式很简单,即通过linq语句查询数据库,得到的结果为数据源,然后进行绑定