首页 > 代码库 > Linq To Sql 增改删
Linq To Sql 增改删
using System; using System.Data.Linq.Mapping; namespace ConsoleApplication3 { [Table(Name = "test")] public class db_test_info { [Column(IsPrimaryKey = true, IsDbGenerated = true)] public int t_id { get; set; } [Column] public string t_name { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data.Linq; namespace ConsoleApplication3 { class Program { private static string m_conn_str = "server=192.168.1.15;database=goods2;uid=sa;pwd=000"; private static void Test<T>(T t) where T : class { Console.WriteLine(t.ToString()); } public static void New(string name) { using (SqlConnection conn = new SqlConnection(m_conn_str)) { using (DataContext dc = new DataContext(conn)) { Table<db_test_info> db = dc.GetTable<db_test_info>(); db_test_info info = new db_test_info() { t_name = name }; db.InsertOnSubmit(info); dc.SubmitChanges(); } } } public static void Update(string name,string newname) { using (SqlConnection conn = new SqlConnection(m_conn_str)) { using (DataContext dc = new DataContext(conn)) { Table<db_test_info> db = dc.GetTable<db_test_info>(); db_test_info info=db.SingleOrDefault(s => s.t_name == name); if (info == null) return; info.t_name=newname; dc.SubmitChanges(); } } } public static void Delete(string name) { using (SqlConnection conn = new SqlConnection(m_conn_str)) { using (DataContext dc = new DataContext(conn)) { Table<db_test_info> db = dc.GetTable<db_test_info>(); db_test_info info = db.SingleOrDefault(s => s.t_name == name); if (info != null) { db.DeleteOnSubmit(info); dc.SubmitChanges(); } } } } public static void DeleteBatchSame(string name) { using (SqlConnection conn = new SqlConnection(m_conn_str)) { using (DataContext dc = new DataContext(conn)) { Table<db_test_info> db = dc.GetTable<db_test_info>(); IEnumerable<db_test_info> result = from s in db where s.t_name == name select s; if (result != null) { db.DeleteAllOnSubmit(result); dc.SubmitChanges(); } } } } static void Main(string[] args) { New("张大拿"); New("刘勇"); New("刘明"); Update("张大拿", "老王"); Update("刘明", "刘勇"); DeleteBatchSame("刘勇"); } } }
Linq To Sql 增改删
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。