目錄
一、添加數(shù)據(jù)
二、修改數(shù)據(jù)
三、刪除數(shù)據(jù)?
四、添加、修改和刪除的源碼
五、生成效果
1.VS和SSMS原始記錄
2.刪除ID=2和5的記錄
3.添加記錄ID=2、5和8
?4.修改ID=3和ID=4的記錄?
????????用LINQtoSQL管理SQL Server數(shù)據(jù)庫時,主要有添加、修改和刪除3種操作。
????????項目中創(chuàng)建LINQtoSQL類的方法已經(jīng)在本文作者的其他文章中有過敘述,此處不再贅述。
一、添加數(shù)據(jù)
????????使用LINQ向SQL Server數(shù)據(jù)庫中添加數(shù)據(jù)時,需要使用InsertOnSubmit()方法和SubmitChanges()方法。其中,InsertOnSubmit()方法用來將處于pending insert狀態(tài)的實體添加到SQL數(shù)據(jù)表中,其語法格式如下:
void InsertOnSubmit(Object entity)?
其中,entity表示要添加的實體。
????????SubmitChanges()方法用來記錄要插入、更新或刪除的對象,并執(zhí)行相應命令以實現(xiàn)對數(shù)據(jù)庫的更改,其語法格式如下:
public void SubmitChanges()
二、修改數(shù)據(jù)
?????????使用LINQ修改SQL Server數(shù)據(jù)庫中的數(shù)據(jù)時,需要用SubmitChanges()方法。
三、刪除數(shù)據(jù)?
????????使用LINQ刪除SQL Server數(shù)據(jù)庫中的數(shù)據(jù)時,需要使用DeleteAllOnSubmit()方法和SubmitChanges()方法。
????????DeleteAllOnSubmit()方法用來將集合中的所有實體置于pending delete狀態(tài)
void DeleteAllOnSubmit(IEnumerable entities)
其中,entities表示要移除所有項的集合。
四、添加、修改和刪除的源碼
//Form1.cs
//使用LINQ管理SQL Server數(shù)據(jù)庫
//對數(shù)據(jù)庫添加、刪除、修改
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();
}
//定義數(shù)據(jù)庫連接字符串
public string strCon = "Data Source=DESKTOP-S11C97H\\SQLEXPRESS;Initial Catalog=db_CSharp;Integrated Security=True;";
DataClasses1DataContext _Linq; //聲明Linq連接對象
public string _strID = ""; //記錄選中的員工編號
/// <summary>
/// 初始化Form1
/// 調(diào)用方法,在datagridview1中顯示數(shù)據(jù)庫
/// </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; //綁定contextMenuStrip1
button2.ContextMenuStrip = contextMenuStrip1; //綁定contextMenuStrip1
contextMenuStrip1.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_Employee
select new
{
info.ID,
info.Name,
info.Sex,
info.Age,
info.Tel,
info.Address,
info.QQ,
info.Email
};
dataGridView1.DataSource = result; //對DataGridView控件進行數(shù)據(jù)綁定
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();
//根據(jù)選中的員工編號獲取其詳細信息,并重新成成一個表
var result = from info in _Linq.tb_Employee
where info.ID == textBox1.Text
select 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_Employee
where employee.ID == _strID
select employee;
_Linq.tb_Employee.DeleteAllOnSubmit(result); //刪除員工信息
_Linq.SubmitChanges(); //實例化Linq連接對象提交操作
MessageBox.Show("員工信息刪除成功");
BindInfo();
}
/// <summary>
/// 按鈕事件:向數(shù)據(jù)庫追加新記錄
/// </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_Employee
where employee.ID == _strID
select 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_Employee
where _employee.ID == textBox1.Text
select _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(); //更新數(shù)據(jù)庫
MessageBox.Show("員工信息修改成功");
BindInfo(); //把修改后的數(shù)據(jù)庫更新到datagridview1中顯示
}
}
}
五、生成效果
1.VS和SSMS原始記錄
??
2.刪除ID=2和5的記錄
?
3.添加記錄ID=2、5和8
?
?4.修改ID=3和ID=4的記錄?
文章來源:http://www.zghlxwxcb.cn/news/detail-745006.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-745006.html
到了這里,關(guān)于C#中使用LINQtoSQL管理SQL數(shù)據(jù)庫之添加、修改和刪除的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!