数据适配器DataAdapter

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 数据适配器DataAdapter//断开式类型
{
    public partial class Form1 : Form
    {
        SqlDataAdapter ad;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“myDBDataSet.student”中。您可以根据需要移动或删除它。
            this.studentTableAdapter.Fill(this.myDBDataSet.student);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=DESKTOP-UJ5IEQK\THIRTEEN;Initial Catalog=myDB;Integrated Security=true";
            SqlCommand com = new SqlCommand("select * from course",con);
             ad = new SqlDataAdapter();
            ad.SelectCommand = com;
            ad.RowUpdating += Ad_RowUpdating;//添加RowUpdating事件,数据更新之前跳出
            ad.RowUpdated += Ad_RowUpdated;//添加RowUpdated事件,数据更新完成后跳出
            SqlCommandBuilder cb = new SqlCommandBuilder(ad);//数据命令生成器对象
             ds = new DataSet();
            //1.自动打开连接,不需要写Open();
            //2.执行完之后,自动关闭连接
            //3.断开连接的数据访问方式
            //4.先执行查询,填充数据集。数据集的结构和select语句相关
            ad.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
            
        }

        private void Ad_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            //throw new NotImplementedException();
            MessageBox.Show("Beginning Update...");
        }

        private void Ad_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
        {
            //throw new NotImplementedException();
            MessageBox.Show("Update Completed");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ad.Update(ds);
            }
            catch 
            {
                //在断开式应用程序中更新数据是,会发成并发性错误,下面是解决方法之一
                if (ds.HasErrors)//测试HasEnors属性
                {
                    foreach (DataTable table in ds.Tables)//检查每个表
                    {
                        if (table.HasErrors)
                        {
                            foreach (DataRow row in table.Rows)
                            {
                                if (row.HasErrors)
                                {
                                    MessageBox.Show("Row:"+row[0],row.RowError);
                                    foreach (DataColumn col in row.GetColumnsInError())//检查表的列
                                    {
                                        MessageBox.Show("Error in this column");

                                    }
                                    row.ClearErrors();//清除错误状态
                                    row.RejectChanges();//拒绝每行的冲突数据

                                }
                            }

                        }

                    }

                }
              
            }
            
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值