解決ADO.NET SQLDataAdapter數(shù)據(jù)庫刪除修改和插入問題
經(jīng)過長時間學(xué)習(xí)ADO.NET,于是和大家分享一下關(guān)于ADO.NET SQLDataAdapter完成對數(shù)據(jù)庫的刪除修改和插入的案例分析,看完本文你肯定可以全面理解ADO.NET的強(qiáng)大之處,大家趕快來看看吧!
#T#本實(shí)驗(yàn)?zāi)繕?biāo)是要求編寫一個應(yīng)用程序,利用ADO.NET SQLDataAdapter對象實(shí)現(xiàn)可以添加、修改、刪除學(xué)生基本信息的功能。數(shù)據(jù)庫為school,共有六個表,該應(yīng)用程序中只使用了表student。數(shù)據(jù)表student中可以先存放一部分?jǐn)?shù)據(jù),便于后面處理。數(shù)據(jù)庫環(huán)境是SQL Server 2005。用鼠標(biāo)雙擊各個Button控件,進(jìn)入.cs文件編輯狀態(tài)準(zhǔn)備進(jìn)行開發(fā)。代碼ADO.NET SQLDataAdapter動態(tài)程序部分如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsApplication1
- {
- public partial class Form9 : Form
- {
- private SqlConnection mycon;
- private SqlDataAdapter myada;
- private SqlCommand mycomd;
- private SqlCommandBuilder mycbd;
- private DataSet myset;
- public Form9()
- {
- InitializeComponent();
- mycon = new SqlConnection("Data Source=LKJ\\SQLEXPRESS;Initial Catalog=school;Integrated Security=True");
- mycomd = new SqlCommand("select * from student",mycon);
- myada = new SqlDataAdapter();
- myada.SelectCommand = mycomd;
- mycbd = new SqlCommandBuilder(myada);
- myset = new DataSet();
- myada.TableMappings.Add("student","student");
- myada.TableMappings[0].ColumnMappings.Add("SNO", "學(xué)號");
- myada.TableMappings[0].ColumnMappings.Add("SNAME", "姓名");
- myada.TableMappings[0].ColumnMappings.Add("SEX", "性別");
- myada.TableMappings[0].ColumnMappings.Add("BIRTHDAY", "生日");
- myada.TableMappings[0].ColumnMappings.Add("CLASS", "班級");
- }
- /// <summary>
- /// 數(shù)據(jù)修改
- /// </summary>
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- //將更改的數(shù)據(jù)更新到數(shù)據(jù)表里
- myada.Update(myset.Tables["student"].GetChanges());
- MessageBox.Show("數(shù)據(jù)庫修改成功","成功信息");
- //DataTable接受更改,以便為下一次更改作準(zhǔn)備
- myset.Tables["student"].AcceptChanges();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- /// <summary>
- /// 初始化數(shù)據(jù)
- /// </summary>
- private void Form9_Load(object sender, EventArgs e)
- {
- try
- {
- myada.Fill(myset, "student");
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.ToString());
- }
- finally
- {
- mycon.Close();
- }
- dataGridView1.DataSource = myset.Tables["student"].DefaultView;
- }
- /// <summary>
- /// 數(shù)據(jù)刪除
- /// </summary>
- private void button2_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("確定要刪除當(dāng)前行數(shù)據(jù)?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
- {
- try
- {
- //從DataTable中刪除當(dāng)前選中的行
- myset.Tables[0].Rows[dataGridView1.CurrentRow.Index].Delete();
- //將更改的數(shù)據(jù)更新到數(shù)據(jù)表里
- myada.Update(myset.Tables[0].GetChanges());
- MessageBox.Show("數(shù)據(jù)刪除成功!");
- //DataTable接受更改,以便為下一次更改作準(zhǔn)備
- myset.Tables[0].AcceptChanges();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- else
- {
- //取消對DataTable的更改
- myset.Tables[0].RejectChanges();
- }
- }
- }

















