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("刘勇");
        }
    }
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。