zoukankan      html  css  js  c++  java
  • C#.NET 数据库连接(Access)

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


    using System.Data.OleDb; //添加引用。

    namespace lianjie
    {
        public partial class Form1 : Form
        {
          
             OleDbConnection strCon = new OleDbConnection();
             OleDbDataAdapter strDA;
             OleDbCommandBuilder strCB;
             DataTable strDT = new DataTable();
             int m_row = 0;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                strCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= E:\Database1.mdb ";
                    strCon.Open();
                    strDA = new OleDbDataAdapter("Select * from LIANXI", strCon);
                    strCB = new OleDbCommandBuilder(strDA);
                    strDA.Fill(strDT);

                    this.showCurrentRecord();
            }

            private void showCurrentRecord()//显示纪录
            {
                if (strDT.Rows.Count == 0)
                {
                    textBox1.Text = "000";
                    textBox2.Text="NO";
                    return;

                }
                textBox1.Text = strDT.Rows[m_row]["ID"].ToString();
                textBox2.Text=strDT.Rows[m_row]["name"].ToString();
            }

            private void button1_Click(object sender, EventArgs e)//第一条记录
            {
                m_row = 0;
                this.showCurrentRecord();
            }

            private void button2_Click(object sender, EventArgs e)//下一条
            {
                if (m_row<strDT.Rows.Count-1)
                {
                    m_row++;
                    this.showCurrentRecord();
                }
            }

            private void button3_Click(object sender, EventArgs e)//上一条
            {
                if (m_row != 0)
                {
                    m_row--;
                    this.showCurrentRecord();
                }
            }

            private void button4_Click(object sender, EventArgs e)//最后一条
            {
                if (strDT.Rows.Count != 0)
                {
                    m_row = strDT.Rows.Count - 1;
                    this.showCurrentRecord();
                }
            }

            private void button5_Click(object sender, EventArgs e)//ADD
            {
                DataRow strRow = strDT.NewRow();
                strRow["ID"] = textBox4.Text;
                strRow["name"] = textBox3.Text;
                strDT.Rows.Add(strRow);
                strDA.Update(strDT);
                m_row = strDT.Rows.Count - 1;
                this.showCurrentRecord();
            }

            private void button6_Click(object sender, EventArgs e)//Delete
            {
                if (strDT.Rows.Count != 0)
                {
                    strDT.Rows[m_row].Delete();
                    strDA.Update(strDT);
                    m_row = 0;
                    this.showCurrentRecord();
                }
            }
        }
    }

  • 相关阅读:
    MySQL 有关权限的表都有哪几个?
    MyISAM 表格将在哪里存储,并且还提供其存储格式?
    你是怎么看Spring框架的?
    elasticsearch 的倒排索引是什么 ?
    主键和候选键有什么区别?
    MySQL 支持事务吗?
    可以使用多少列创建索引?
    LIKE 声明中的%和_是什么意思?
    什么是通用 SQL 函数?
    MySQL 中有哪些不同的表格?
  • 原文地址:https://www.cnblogs.com/cuishao1985/p/1343730.html
Copyright © 2011-2022 走看看