OleDbDataAdapter 具体使用案例



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

namespace TestUDL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbDataAdapter ad;
        DataSet ds;
        public void fanfa() {
            //string connectString = "Data Source=localhost;Initial Catalog=MytestDB;Integrated Security=SSPI";
            //SqlConnection sqlConnect = new SqlConnection(connectString);
            string connectString = @"File Name=D:\VS2015Programming\MyTest\TestUDL\TextFile1.udl;";
            OleDbConnection conn = new OleDbConnection(connectString);
            conn.Open();
            OleDbCommand sqlCommand = conn.CreateCommand();
            string sqlCommandText = "SELECT [ID],[Name] FROM[MytestDB].[dbo].[PersonTable]";
            sqlCommand.CommandText = sqlCommandText;
            //OleDbDataReader sqlDataReader = sqlCommand.ExecuteReader();
            ad = new OleDbDataAdapter(sqlCommand);

            //重要的数据更新操作,下面的OleDbDataAdapterUpdate会用到,可以自懂执行数据库操作
            OleDbCommandBuilder bb = new OleDbCommandBuilder(ad);
            ad.UpdateCommand = bb.GetUpdateCommand();
            ad.InsertCommand = bb.GetInsertCommand();
            ad.DeleteCommand = bb.GetDeleteCommand();
             ds = new DataSet();
            ad.Fill(ds);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string connectString = "Data Source=localhost;Initial Catalog=MytestDB;Integrated Security=SSPI";
            SqlConnection sqlConnect = new SqlConnection(connectString);
            //string connectString = @"File Name=D:\VS2015Programming\MyTest\TestUDL\TextFile1.udl;";
            //OleDbConnection conn = new OleDbConnection(connectString);
            //conn.Open();
            //OleDbCommand sqlCommand = conn.CreateCommand();
            //string sqlCommandText = "SELECT [ID],[Name] FROM[MytestDB].[dbo].[PersonTable]";
            //sqlCommand.CommandText = sqlCommandText;
            OleDbDataReader sqlDataReader = sqlCommand.ExecuteReader();
            //OleDbDataAdapter ad = new OleDbDataAdapter(sqlCommand);
            //DataSet ds = new DataSet();
            //ad.Fill(ds);
            fanfa();
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTableCollection ss = ds.Tables;
            //ds.Tables[0].Rows[5].RowState = DataRowState.Added;
            this.ad.Update(ds.Tables[0]);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值