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 增改删的更多相关文章
- LINQ to SQL 增,删,改
添加 InsertOnSubmit(单个对象) 或 InsertAllOnSubmit(集合) 删除 DeleteOnSubmit (单个对象) DeleteAll ...
- LinQ to SQL 增,删,改 代码演示
NorthwindDBDataContext dc = new NorthwindDBDataContext(); protected void Page_Load(object sender, Ev ...
- XML简单的增改删操作
XML文件的简单增改删,每一个都可以单独拿出来使用. 新创建XML文件,<?xmlversion="1.0"encoding="utf-8"?> & ...
- js 属性增改删操作
js 属性增改删操作,可参看菜鸟教程,这里记录一个小问题:disabled属性 使用setAttribute操作无法 禁用disabled属性,需使用removeAttribute操作,原因是只要有d ...
- DataFrame查增改删
DataFrame查增改删 查 Read 类list/ndarray数据访问方式 dates = pd.date_range(',periods=10) dates df = pd.DataFrame ...
- Linq to sql 增删改查(转帖)
http://blog.csdn.net/pan_junbiao/article/details/7015633 (LINQ To SQL 语法及实例大全) 代码 Code highlightin ...
- linq to sql 增删改查
ORM<Object Relation Mapping> Linq To Sql: 一.建立Linq To Sql 类 : 理解上下文类: Linq To Sql 类名+context 利 ...
- sharepoint Linq方式的增,删,查,改
Site9527EntitiesDataContext (重要的类):连接实体与网站List操作 SPContext.Current.Web.Url:获取当前操作的页面 FirstOrDefault: ...
- SQL 增、删、改、查语句
1.SQL SELECT 语句 SELECT语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL SELECT语法 SELECT 列名称 FROM 表名称 以及 SELECT * ...
随机推荐
- [转载]MyBatis mapper文件中的变量引用方式#{}与${}的差别
转载自:http://blog.csdn.net/szwangdf/article/details/26714603 默认情况下,使用#{}语法,MyBatis会产生PreparedStatement ...
- 【软件构造】(转)Java中的comparable和comparator
为了方便阅读和复习,转载至此,原地址:温布利往事的博客 阅读目录 一.Comparable简介 二.Comparator简介 三.Comparable和Comparator区别比较 回到顶部 一.Co ...
- 10C++类和对象
类和对象 8.1 面向对象程序设计方法概述 到目前为止,我们介绍的是C++在面向过程的程序设计中的应用.对于规模比较小的程序,编程者可以直接编写出一个面向过程的程序,详细地描述每一瞬时的数据结构及对其 ...
- Java中a=a++ 和 a=++a(转)
转自https://blog.csdn.net/lovepluto/article/details/81062176 如果问 a++ 和 ++a 的区别,估计很多都能回答上来.a++ 是先取 a 的值 ...
- Bullet:MySQL增强半同步参数rpl_semi_sync_master_wait_point值AFTER_SYNC和AFTER_COMMIT的对比实验
MySQL 5.7.22启用增强半同步复制 MySQL对该参数值的描述 Semisync can wait for slave ACKs at one of two points, AFTER_SYN ...
- CF1065D Three Pieces
题目描述:给出一个n*n的棋盘,棋盘上每个格子有一个值.你有一个子,要求将这个子从1移到n*n(去k时可以经过比k大的点). 开局时它可以作为车,马,相(国际象棋).每走一步耗费时间1.你也可以中途将 ...
- (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d
出现redis错误: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to p ...
- Go:二分查找
package main import "fmt" func BinarySearch(arr *[5]int, leftIndex int, rightIndex int, fi ...
- pycharm中提交Git 忽略部分代码
痛点: 项目中,有些配置项,或者比较隐私的东东,不想上传 解决:在项目根路径下,创建.gitignore 文件 文件中可以写文件名.文件路径等 结果: 提交到git,发现果真没有dbconne ...
- Python之trutle库-五角星
Python之trutle库-五角星 #!/usr/bin/env python # coding: utf-8 # Python turtle库官方文档:https://docs.python.or ...