Linq To Sql 增改删

Posted lxjshuju

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 增改删的主要内容,如果未能解决你的问题,请参考以下文章

django入门 11 网页关联MySQL单字段表(增改删)

MyBatis中sqlSession操作数据库,不报错但无法实现数据修改(增改删)

dljd_027_增改删在同一个事务中的默认执行顺序

使用缓存

数据库-SQL教程

数据库-SQL教程