请教c#连接access
请问怎么用c#连接access数据库!最好能有完整的代码!我要做的是密码登陆,最简单的就是2个编辑框和一按钮,编辑框分别是账号输入和密码输入,按钮是登陆!触发button事件后连接到access数据库,判断2个编辑框里输入的账号密码是否正确! 希望大虾们帮帮我这个菜鸟!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;//与Access数据库连接
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
{
MessageBox.Show("对不起,您还没有输入用户名或密码", "提示");
textBox1.Focus();
}
else
{
//OleDbConnection类用来连接Access数据库
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\zhmi\My Documents\myUser.mdb");
//打开数据库
conn.Open();
OleDbDataAdapter dauser = new OleDbDataAdapter("select * from 密码 where user='" + textBox1.Text.Trim() + "'", conn);
DataSet dsuser = new DataSet();
dauser.Fill(dsuser, "密码");
if (dsuser.Tables[0].Rows.Count == 0)
{
MessageBox.Show("对不起,您还没有注册,请先注册", "提示");
textBox1.Clear();
textBox2.Clear();
textBox1.Focus();
}
else
{
DataRow[] dr = dsuser.Tables[0].Select("user='" + textBox1.Text + "'");
if ((dr[0]["pwd"].ToString(), textBox2.Text) == 0)
{
MessageBox.Show("登录成功", "提示");
}
else
{
MessageBox.Show("密码错误,请重新输入", "提示");
textBox2.Clear();
textBox2.Focus();
}
}
}
}
}
}