C#连接access及相关操作

这篇博客介绍了如何使用C#连接Access数据库,并进行数据查询和显示。通过建立数据库连接,填充DataSet,然后将数据绑定到ComboBox,实现了从数据库中获取用户名的展示。此外,还展示了如何根据用户输入的用户名和密码进行登录验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
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)
        
{
            
//Login WinForm no change size
            this.FormBorderStyle =System.Windows.Forms.FormBorderStyle.FixedSingle;
            
/*
            //Method : C# Connect Access DataBase;
            System.Data.OleDb.OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=D: est.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();
            }
            
*/


            
// ComboBox与数据库的绑定
            
// 1、创建数据库连接
            OleDbConnection dbconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=D: est.mdb");
            
// 2、创建一个myDataSet
            DataSet myDataSet = new DataSet();
            dbconn.Open();
            
// 3、用OleDbDataAdapter得到一个数据集
            OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT 用户名 FROM login", dbconn);
            
// 4、myDataSet绑定数据表
            myCommand.Fill(myDataSet, "login");
            
// 5、关闭连接
            dbconn.Close();
            
// 6、榜定ComboBox组件
            this.comboBox1.DataSource = myDataSet;
            
this.comboBox1.DisplayMember ="login.用户名";
            
this.comboBox1.ValueMember ="login.用户名";   
        }


        
        
private void button1_Click(object sender, EventArgs e)
        
{
            
if (textBox1.Text == "" || textBox2.Text =="")
            
{
                MessageBox.Show(
"Please input UserName or PassWord !"this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                
return;
            }


            OleDbConnection dbconn 
= new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=D: est.mdb");
            OleDbCommand myCmd 
= new OleDbCommand("SELECT * FROM login WHERE 用户名='" + textBox1.Text + "'", dbconn);
            dbconn.Open();
            OleDbDataReader reader;
            reader 
= myCmd.ExecuteReader();

            
if (reader.Read())
            
{
                
if (textBox2.Text == reader["密码"].ToString())
                
{
                    
//MessageBox.Show(textBox1.Text  + reader["密码"].ToString() + "成功登陆!",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Form2 frm2 = new Form2();
                    frm2.Show();
                    
this.Hide();
                    
//dbconn.Close();
                }

            }

            
else
            
{
                MessageBox.Show(
"没有这个用户!",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                
            }


            dbconn.Close();
        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            Application.Exit();
        }


        
private void Form1_Closing(object sender,System.ComponentModel.CancelEventArgs e)
        
{
            
//e.Cancel = true;
        }


    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值