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 增改删)