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

  • 相关阅读:
    名称空间与作用域
    3.19作业
    函数的参数
    文件的f.seek和文件修改方式以及函数的基本使用
    3.17作业
    文件处理
    3.16作业
    转 移动端-webkit-user-select:none导致input/textarea输入框无法输入
    移动端开发兼容性总结
    移动端input 无法获取焦点的问题
  • 原文地址:https://www.cnblogs.com/cuishao1985/p/1343730.html
Copyright © 2020-2023  润新知