您的位置:首页 > 编程语言 > C#

C#连接access及相关操作

2008-01-22 09:15 351 查看

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;


}




}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: