且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问MS Access数据库

更新时间:2022-11-25 14:24:12

有很多示例.请不要忘记: ^ ]! :)
There are plenty of examples. Please do not forget: google is your friend[^]! :)


using System.Data.OleDb; //use this namespace
//create object of the oledb class
OleDbConnection con = new OleDbConnection();
//enter conncetion string via udl file expalnation given below

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\database.mdb;Persist Security Info=False" 
//Open the connection                        
con.Open();
//Her''s the query
string command ="select eid from Inquiry_Table";
OleDbCommand cmd = new OleDbCommand(command, con);
OleDbDataReader dr = cmd.ExecuteReader(); //executing the query

while(dr.Read()==true)
{
cboempid.Items.Add(dr[0].ToString());
}
con.Close();//closing the conncetion
con.Dispose();//removing connection object from memory




上面的代码将从访问数据库中读取值,并将数据添加到组合框中.

创建一个记事本文件并将其保存为扩展名.udl将文件打开到提供程序部分,选择Microsoft.Jet.OLEDB.4.0,然后浏览到您的访问文件的位置,单击测试连接,然后单击完成,以获取连接字符串执行上述步骤,然后使用记事本打开udl文件


对您的回答进行评分后,一旦发现有帮助
谢谢&问候;
基数.




The above code will read the value from an access database and will add data into the combobox

create a notepad file and save it with the extention .udl open the file into the provider section select Microsoft.Jet.OLEDB.4.0 and then browse for the location of ur access file click on test connection and ur done to go and inorder to get the connection string performn the above steps and then open the udl file with notepad


rate my answer once u find it helpful
Thanks & Regards;
Radix.