非常帅气的Linq to sql
using System;
using System.Linq;
#region MYSQL
using MySql.Data.MySqlClient;
#endregion
namespace LinqTest {
class Program {
static void Main(string[] args) {
// MySql的初始化
string connStr = @"server=localhost;database=myblog;user=root;pwd=******(这个同样要匿);port=3306";
MySqlConnection conn = new MySqlConnection(connStr);
var tm = new MyBLog(conn);
// Sql Server Express 的初始化
var t = new DataClassesDataContext();
// 第一个表
var rs1 = from sample in t.sample
select sample;
foreach (var i in rs1) {
Console.WriteLine("id: " + i.ID + " val: " + i.值);
}
// 第二个表
var rs2 = from Sample2 in t.Sample2
where Sample2.相关ID > 1
select Sample2;
var rl2 = rs2.ToList();
foreach (var i in rl2) {
Console.WriteLine("id: " + i.PID + " sid: " + i.相关ID + " date: " + i.日期);
}
// 插入操作
Sample2 s2 = new Sample2() {
PID = rl2.Last().PID + 1,
相关ID = rl2.Last().相关ID,
日期 = DateTime.Now
};
t.Sample2.InsertOnSubmit(s2); // 木有错,这样就标记为插入了
t.SubmitChanges();// 因为之后没有查询,所以这里要Submit一下,否则不会进数据库
// MySql和Dblinq
var rs3 = (from WpCommentMeta in tm.WpCommentMeta
where WpCommentMeta.MetaID > 10
orderby WpCommentMeta.CommentID
select WpCommentMeta).Skip(3).Take(3);
foreach (var i in rs3.ToList()) {
Console.WriteLine("MetaID: " + i.MetaID + " CommentID: " + i.CommentID);
Console.WriteLine("Key:\r\n" + i.MetaKey + "\r\nValue:\r\n" + i.MetaValue);
Console.WriteLine("");
}
}
}
}Last updated
Was this helpful?