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 * ...
随机推荐
- vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改
vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改.vue <div class="commonHeader" v-bind:class=" ...
- 微信小程序图片上传java后台(前后端代码)
小程序代码 upload:function(e){ var that = this; wx.showActionSheet({ itemList: ['从相册选择','拍照'], itemColor: ...
- fastclick.js插件使用
引入插件步骤 ①在HTML页面中添加 <script type='application/javascript' src='/path/to/fastclick.js'></scr ...
- buf.compare()
buf.compare(otherBuffer) otherBuffer {Buffer} 返回:{Number} 比较两个 Buffer 实例,无论 buf 在排序上靠前.靠后甚至与 otherBu ...
- thinkphp5 框架修改的地方
框架修改的地方 vendor/topthink/think-captcha/src/Captcha.php api验证码入库 196行 $img_code = strtoupper(implode(' ...
- eclipse安装Aptana 插件,并设置使之能提示css,js,html,帮助编写代码
在Eclipse 4.2 上安装 Aptana 3.2遇到的错误 就是找不到什么文件来着,我在装maven的时候也遇到了. 烦人... (这文章是我还在用eclipse的时候,为了编写js代码的时候提 ...
- 上传文件表单file,限制上传文件类型的方法--参数accept
我们使用<input type="file" />来上传文件,但是当你只想要上传某种格式的文件,比如说(jpg)文件时.可以通过accept来限制. <form& ...
- linux程序分析工具
ldd和nm是Linux下两个非常实用的程序分析工具.ldd是用来分析程序运行时需要依赖的动态链接库的工具,nm是用来查看指定程序中的符号表信息的工具,objdump用来查看源代码与汇编代码,-d只查 ...
- 关于FFM (Field-aware Factorization Machines) 的几篇博客
深入FFM原理与实践 http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html#mjx-eqn-e ...
- Sum-(最大子序列和)
http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=652&pid=1003 题目大意: 给你一个序列,你随便找一 ...