using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace MyAccessConnectionTest
{
public partial class Form1 : Form
{
Form2 ShowInformationForm;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//接收文本框中输入的用户信息
string UserIDSave=textBox1 .Text.ToString () ;
string UserKeySave=textBox2 .Text.ToString () ;
//创建连接access数据库的连接字符串
string ConnectionWords = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Users\learic\Desktop\MyAccessConnectionTest.mdb";
//连接数据库
OleDbConnection MyAccessConnect = new OleDbConnection(ConnectionWords);
//打开数据库
MyAccessConnect.Open();
//数据库命令字符串
string CommandString = string.Format("SELECT UserID,UserKey FROM UserLogTable WHERE (erID='{0}') AND (erKey='{1}')", UserIDSave, UserKeySave);
//string CommandString = "SELECT UserID,UserKey FROM UserLogTable WHERE UserID=" + UserIDSave + " AND UserKey=" + UserKeySave + "";
//string CommandString = "SELECT UserID,UserKey FROM UserLogTable WHERE UserID=" +
UserIDSave + " AND UserKey=" + UserKeySave + "";
//string CommandString = "SELECT Count(*) FROM UserLogTable WHERE UserID=" + UserIDSave + " AND UserKey=" + UserKeySave + "";
//string CommandString = string.Format("select UserID,UserKey from UserLogTable where UserID={0}");
//OleDbCommand Cmd = new OleDbCommand(CommandString ,MyAccessConnect );
OleDbCommand Cmd = MyAccessConnect.CreateCommand();
//OleDbTransaction transaction = null;
//transaction = MyAccessConnect.BeginTransaction();
//Cmd.Transaction = transaction;
mandText = CommandString;
//mit();
//Access数据库适配器
OleDbDataAdapter GetInformationFromAccess = new OleDbDataAdapter(Cmd);
//创建一个Access表空间
DataSet TempTable = new DataSet();
//将适配器中取出的信息存储到计算机中创建的表空间中去
GetInformationFromAccess.Fill(TempTable);
//OleDbDataReader ReadMyaccess = Cmd.ExecuteReader();
//关闭数据库
MyAccessConnect.Close();
//if (ReadMyaccess.Read() == true)
//{
// if (ReadMyaccess.GetInt32(0) > 0)
// {
// this.Hide();
// this.ShowInformationForm = new Form2();
// this.ShowInformationForm.ShowInformation.Text = "登录成功";
// this.ShowInformationForm.ShowDialog();
// this.Dispose();
// }
// else
// {
// this.Hide();
// this.ShowInformationForm = new Form2();
// this.ShowInformationForm.ShowInformation.Text = "用户名或者密码错误";
// this.ShowInformationForm.ShowDialog();
// this.Dispose();
// }
//}
//else
//{
// this.Hide();
// this.ShowInformationForm = new Form2();
// this.ShowInformationForm.ShowInformation.Text = "用户未注册";
// this.ShowInformationForm.ShowDialog();
// this.textBox1.Text = "";
// this.textBox2.Text = "";
// this.ShowInformationForm.ShowDialog();
// this.Show();
//}
//ReadMyaccess.Close();
//判断创建的数据库表是否为空从而判断数据库中是否有该信息
if (TempTable != null && TempTable.Tables[0].Rows.Count > 0)
{
this.Hide();
this.ShowInformationForm = new Form2();
this.ShowInformationForm.ShowInformation.Text =
TempTable.Tables[0].Rows[0]["UserID"].ToString();
this.ShowInformationForm.ShowDialog();
this.Dispose();
}
else
{
this.Hide();
this.ShowInformationForm = new Form2();
this.ShowInformationForm.ShowInformation.Text = "登录失败";
this.ShowInformationForm.ShowDialog();
this.textBox1.Text = "";
this.textBox2.Text = "";
this.Show();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}。