关闭

C#连接Access数据库

标签: accessc#logindatasetdatabasetextbox
2905人阅读 评论(2) 收藏 举报

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;//Add
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //Method 1: C# Connnect Access DataBase; *** Don't Success ***
            /* string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=D:/test.mdb";
            string strSQL = "SELECT * FROM login";
            System.Data.OleDb.OleDbConnection myConn = new OleDbConnection(strconn);    
            System.Data.OleDb.OleDbDataAdapter myAper = new OleDbDataAdapter(strSQL,myConn);
            System.Data.DataSet myset = new DataSet();
            myAper.Fill (myset,"login");
            this.dataGridView1.DataSource = myset.Tables["login"];
            */

            //Method 2: C# Connect Access DataBase;
            System.Data.OleDb.OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=D:/test.mdb");
            //dbconn.Open();
            System.Data.OleDb.OleDbCommand myCmd = new OleDbCommand("select 用户名 from login", dbconn);
            System.Data.OleDb.OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCmd);
            System.Data.DataSet myDataSet = new DataSet();
            //myAd.Fill (myDs,"login");
            try
            {
                dbconn.Open();
                myAdapter.Fill(myDataSet,"login");
            }
            catch(OleDbException)
            {
                //错误处理
            }
            finally
            {
                if (dbconn.State == ConnectionState.Open)
                    dbconn.Close();
            }
            this.dataGridView1.DataSource = myDataSet.Tables["login"];
            //this.dataGridViewComboBoxEditingControl1.DataSource = myDataSet.Tables["login"];
           
        }
    }
}

 

[OleDbDataReader 的运用]

            OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=D:/test.mdb");
            OleDbCommand myCmd = new OleDbCommand("select * from login where 用户名='" + textBox1.Text + "'", dbconn);
            dbconn.Open();
            OleDbDataReader reader;
            reader = myCmd.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show(reader["密码"].ToString());
            }
            else
            {
                MessageBox.Show("没有这个用户!");
            }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:142604次
    • 积分:2088
    • 等级:
    • 排名:第18348名
    • 原创:61篇
    • 转载:12篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论
    友情链接