目录
一、添加数据
二、修改数据
三、删除数据
四、添加、修改和删除的源码
五、生成效果
1.VS和SSMS原始记录
2.删除ID=2和5的记录
3.添加记录ID=2、5和8
4.修改ID=3和ID=4的记录
用LINQtoSQL管理SQL Server数据库时,主要有添加、修改和删除3种操作。
项目中创建LINQtoSQL类的方法已经在本文作者的其他文章中有过叙述,此处不再赘述。
一、添加数据
使用LINQ向SQL Server数据库中添加数据时,需要使用InsertOnSubmit()方法和SubmitChanges()方法。其中,InsertOnSubmit()方法用来将处于pending insert状态的实体添加到SQL数据表中,其语法格式如下:
void InsertOnSubmit(Object entity)
其中,entity表示要添加的实体。
SubmitChanges()方法用来记录要插入、更新或删除的对象,并执行相应命令以实现对数据库的更改,其语法格式如下:
public void SubmitChanges()
二、修改数据
使用LINQ修改SQL Server数据库中的数据时,需要用SubmitChanges()方法。
三、删除数据
使用LINQ删除SQL Server数据库中的数据时,需要使用DeleteAllOnSubmit()方法和SubmitChanges()方法。
DeleteAllOnSubmit()方法用来将集合中的所有实体置于pending delete状态
void DeleteAllOnSubmit(IEnumerable entities)
其中,entities表示要移除所有项的集合。
四、添加、修改和删除的源码
//Form1.cs
//使用LINQ管理SQL Server数据库
//对数据库添加、删除、修改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;namespace _07
{public partial class Form1 : Form{public Form1(){InitializeComponent();}//定义数据库连接字符串public string strCon = "Data Source=DESKTOP-S11C97H\\SQLEXPRESS;Initial Catalog=db_CSharp;Integrated Security=True;";DataClasses1DataContext _Linq; //声明Linq连接对象public string _strID = ""; //记录选中的员工编号/// <summary>/// 初始化Form1/// 调用方法,在datagridview1中显示数据库/// </summary>private void Form1_Load(object sender, EventArgs e){label1.Text = "编码:";label2.Text = "姓名:";label3.Text = "年龄:";label4.Text = "电话:";label5.Text = "地址:";label6.Text = "QQ:";label7.Text = "EMAIL:";label8.Text = "性别:";button1.Text = "添加";button2.Text = "删除";button3.Text = "修改";groupBox1.Text = "员工信息";comboBox1.Items.AddRange(new object[] {"男","女"});textBox1.Size = new System.Drawing.Size(100,21);textBox2.Size = new System.Drawing.Size(100, 21);textBox3.Size = new System.Drawing.Size(40, 21);textBox4.Size = new System.Drawing.Size(100, 21);textBox5.Size = new System.Drawing.Size(100, 21);textBox6.Size = new System.Drawing.Size(100, 21);textBox7.Size = new System.Drawing.Size(100, 21);comboBox1.Size = new System.Drawing.Size(40, 21);button1 .Size = new Size(50, 21);button2.Size = new Size(50, 21);button3.Size = new Size(50, 21);dataGridView1.AllowUserToAddRows = true;dataGridView1.AllowUserToDeleteRows = true;dataGridView1.AllowUserToResizeColumns = true ;dataGridView1.AllowUserToResizeRows = false;dataGridView1.RowHeadersVisible = false; dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect;dataGridView1.ReadOnly = false;dataGridView1.ContextMenuStrip = contextMenuStrip1; //绑定contextMenuStrip1button2.ContextMenuStrip = contextMenuStrip1; //绑定contextMenuStrip1contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {toolStripMenuItem1});toolStripMenuItem1.Text = "删除";toolStripMenuItem1.Size = new Size(40,21);BindInfo(); }#region 显示所有员工信息/// <summary>/// 显示所有员工信息/// </summary>private void BindInfo(){_Linq = new DataClasses1DataContext(strCon); //实例化Linq连接对象//获取所有员工信息var result = from info in _Linq.tb_Employeeselect new{info.ID,info.Name,info.Sex,info.Age,info.Tel,info.Address,info.QQ,info.Email};dataGridView1.DataSource = result; //对DataGridView控件进行数据绑定dataGridView1.Columns[0].Width = 60;dataGridView1.Columns[1].Width = 60;dataGridView1.Columns[2].Width = 30;dataGridView1.Columns[3].Width = 30;dataGridView1.Columns[4].Width = 80;dataGridView1.Columns[5].Width = 150;dataGridView1.Columns[6].Width = 80;dataGridView1.Columns[7].Width = 150;}#endregion/// <summary>/// 鼠标点击cell,获得选中行的编号/// </summary>private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){//获取选中的员工编号,给删除事件使用_strID = Convert.ToString(dataGridView1[0, e.RowIndex].Value).Trim();//以下,给修改事件使用_Linq = new DataClasses1DataContext(strCon); //实例化Linq连接对象//获取选中的员工编号textBox1.Text = Convert.ToString(dataGridView1[0, e.RowIndex].Value).Trim();//根据选中的员工编号获取其详细信息,并重新成成一个表var result = from info in _Linq.tb_Employeewhere info.ID == textBox1.Textselect new{info.ID,info.Name,info.Sex,info.Age,info.Tel,info.Address,info.QQ,info.Email};//相应的文本框及下拉列表中显示选中员工的详细信息foreach (var item in result){textBox2.Text = item.Name;comboBox1.Text = item.Sex;textBox3.Text = item.Age.ToString();textBox4.Text = item.Tel;textBox5.Text = item.Address;textBox6.Text = item.QQ.ToString();textBox7.Text = item.Email;}}/// <summary>/// ToolStripMenuItem1控件的删除事件/// </summary>private void ToolStripMenuItem1_Click(object sender, EventArgs e){if (_strID == ""){MessageBox.Show("请选择要删除的记录");return;}_Linq = new DataClasses1DataContext(strCon);//实例化Linq连接对象//查找要删除的员工信息var result = from employee in _Linq.tb_Employeewhere employee.ID == _strIDselect employee;_Linq.tb_Employee.DeleteAllOnSubmit(result); //删除员工信息_Linq.SubmitChanges(); //实例化Linq连接对象提交操作MessageBox.Show("员工信息删除成功");BindInfo();}/// <summary>/// 按钮事件:向数据库追加新记录/// </summary>private void Button1_Click(object sender, EventArgs e){_Linq = new DataClasses1DataContext(strCon); //实例化Linq连接对象tb_Employee _employee = new tb_Employee{//为tb_Employee类中的员工实体赋值ID = textBox1.Text,Name = textBox2.Text,Sex = comboBox1.Text,Age = Convert.ToInt32(textBox3.Text),Tel = textBox4.Text,Address = textBox5.Text,QQ = Convert.ToInt32(textBox6.Text),Email = textBox7.Text}; //实例化tb_Employee类对象_Linq.tb_Employee.InsertOnSubmit(_employee); //删除员工信息_Linq.SubmitChanges(); //实例化Linq连接对象提交操作MessageBox.Show("员工信息添加成功");BindInfo();}/// <summary>/// 按钮事件:删除选中的记录/// new方法新定义的实例不能用于修改,因为new后的实例用的是新的主键值/// 只可以追加记录,不可以修改记录,对已有记录进行修改应用foreach方法/// </summary>private void Button2_Click(object sender, EventArgs e){_Linq = new DataClasses1DataContext(strCon); //实例化Linq连接对象//tb_Employee _employee = new tb_Employee//{// //为tb_Employee类中的员工实体赋值// ID = textBox1.Text, // Name = textBox2.Text,// Sex = comboBox1.Text,// Age = Convert.ToInt32(textBox3.Text),// Tel = textBox4.Text,// Address = textBox5.Text,// QQ = Convert.ToInt32(textBox6.Text),// Email = textBox7.Text//}; //实例化tb_Employee类对象if (_strID == ""){MessageBox.Show("请选择要删除的记录");return;}_Linq = new DataClasses1DataContext(strCon);//实例化Linq连接对象//查找要删除的员工信息var result = from employee in _Linq.tb_Employeewhere employee.ID == _strIDselect employee;_Linq.tb_Employee.DeleteAllOnSubmit(result); //删除员工信息_Linq.SubmitChanges(); //实例化Linq连接对象提交操作MessageBox.Show("员工信息删除成功");BindInfo();}/// <summary>/// 按钮事件:修改选中的记录/// </summary>private void Button3_Click(object sender, EventArgs e){_Linq = new DataClasses1DataContext(strCon); //实例化Linq连接对象if (textBox1.Text == ""){MessageBox.Show("请选择要修改的记录");return;}//查找要修改的员工信息var result = from _employee in _Linq.tb_Employeewhere _employee.ID == textBox1.Textselect _employee;//对指定的员工信息进行修改foreach (tb_Employee _employee in result){_employee.Name = textBox2.Text;_employee.Sex = comboBox1.Text;_employee.Age = Convert.ToInt32(textBox3.Text);_employee.Tel = textBox4.Text;_employee.Address = textBox5.Text;_employee.QQ = Convert.ToInt32(textBox6.Text);_employee.Email = textBox7.Text;}_Linq.SubmitChanges(); //更新数据库MessageBox.Show("员工信息修改成功");BindInfo(); //把修改后的数据库更新到datagridview1中显示}}
}
五、生成效果
1.VS和SSMS原始记录
2.删除ID=2和5的记录
3.添加记录ID=2、5和8