Add data to the Access database using ADO
2024-09-07 19:04:54
供稿:网友
öð¹ú×î´óµäweb¿ª·¢×êô´íøõ¾¼°¼¼êõéççø£¬
add data to the database using ado
tools used : visual c# .net
this program shows how to insert data in an access database using ado and sql query. i have an access 2000 database in project/bin/debug directory. my database name is mctest.mdb. you can create your own database and copy in your project's debug directory. i am not attaching database with this code because of size problem.
i have a table 'developer' in this database which has four columns. i am inserting values into two columns name and address in this code.
using system;
using system.data;
using system.data.oledb;
namespace adonetwritequery
{
/// <summary>
/// summary description for class1.
/// </summary>
class class1
{
static void main(string[] args)
{
string strdsn = "provider=microsoft.jet.oledb.4.0;data
source=c://mctest.mdb";
string strsql = "insert into developer(name, address ) values(
'newname', 'newaddress')" ;
// create objects of adoconnection and adocommand
oledbconnection myconn = new oledbconnection(strdsn);
oledbcommand mycmd = new oledbcommand( strsql, myconn );
try
{
myconn.open();
mycmd.executenonquery();
}
catch (exception e)
{
console.writeline("oooops. i did it again:/n{0}", e.message);
}
finally
{
myconn.close();
}
}
}
}