AdddatatothedatabaseusingADO
ToolsUsed:VisualC#.Net
ThisprogramshowshowtoinsertdatainanaccessdatabaseusingADOandSQLquery.IhaveanAccess2000databaseinprojectindebugdirectory.MydatabasenameismcTest.MDB.Youcancreateyourowndatabaseandcopyinyourproject'sdebugdirectory.IamnotattachingdatabasewiththiscodebecauSEOfsizeproblem.
Ihaveatable'Developer'inthisdatabasewhichhasfourcolumns.IaminsertingvaluesintotwocolumnsNameandAddressinthiscode.
usingSystem;
usingSystem.Data;
usingSystem.Data.OleDb;
namespaceADONETWriteQuery
{
///<summary>
///SummarydescriptionforClass1.
///</summary>
classClass1
{
staticvoidMain(string[]args)
{
stringstrDSN="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\mcTest.MDB";
stringstrSQL="INSERTINTODeveloper(Name,Address)VALUES(
'NewName','NewAddress')";
//createObjectsofADOConnectionandADOCommand
OleDbConnectionmyConn=newOleDbConnection(strDSN);
OleDbCommandmyCmd=newOleDbCommand(strSQL,myConn);
try
{
myConn.Open();
myCmd.ExecuteNonQuery();
}
catch(Exceptione)
{
Console.WriteLine("Oooops.Ididitagain:{0}",e.Message);
}
finally
{
myConn.Close();
}
}
}
}