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();
                }
            }
        }
    }

  • 相关阅读:
    Java 装饰者模式
    struts2注解的作用
    XML DOM 笔记
    XMLHttpRequest的用法
    Eclipse中实现JS代码提示功能
    .after()和.before()的关系
    xml的的特殊字符转义&
    html和xml的区别
    dom4j的解析实例
    tld自定义标签系列--使用body-content的作用--比较有用
  • 原文地址:https://www.cnblogs.com/cuishao1985/p/1343730.html
Copyright © 2011-2022 走看看