时间:2021-07-01 10:21:17 帮助过:17人阅读
新增数据
string str = "server=localhost;database=my_db;uid=sa;pwd=123";
DataClasses1DataContext db = new DataClasses1DataContext(str);
Product tb = new Product(); // 创建表对象
tb.ProductName = "充电器";
tb.ProductPrice = 50;
tb.ProduceAdressID = 4;
db.Product.InsertOnSubmit(tb); // 生成sql脚本
db.SubmitChanges(); // 执行sql脚本,保存数据
修改数据
string str = "server=localhost;database=my_db;uid=sa;pwd=123";
DataClasses1DataContext db = new DataClasses1DataContext(str);
var item = from s in db.Product where s.ProductID == 7 select s;
item.FirstOrDefault().ProductName = "手机充电器";
db.SubmitChanges(); // 执行sql脚本,保存数据
删除数据
string str = "server=localhost;database=my_db;uid=sa;pwd=123";
DataClasses1DataContext db = new DataClasses1DataContext(str);
var item = from s in db.Product where s.ProductID == 7 select s;
db.Product.DeleteOnSubmit(item.FirstOrDefault());
db.SubmitChanges(); // 执行sql脚本,保存数据
linq to sql 类的使用
标签:轻量级 res sse new select 手机 sub cti body