Add data to the Access database using ADO
2008-05-01 02:43
543 查看
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();
}
}
}
}
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();
}
}
}
}
![](http://blog.csdn.net/upimg/20061210/11AID0422054029.jpg)
相关文章推荐
- Python Web-第四周-Programs that Surf the Web(Using Python to Access Web Data)
- Failed to start the database service MSSQL$OfficeServers. Repair this product using Add/Remove Programs
- Using the data layer API to query your Kentico database
- How to access database using ADO in VB6 - 在 VB6 中用ADO对象如何访问数据库
- Python Web-第六周-JSON and the REST Architecture(Using Python to Access Web Data)
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- How to access database using C++ & ADO - 如何用C++和ADO访问数据库
- 2 - Much ADO about Data Access: Using the Data Access Application Block
- Access to the database file is not allowed. [ File name = ***\DataBase.sdf
- Data Collection and Storage We noticed that your app requests the user’s consent to access the ....
- Collecting performance counters and using SQL Server to analyze the data
- Unloading Data Using the ORACLE_DATAPUMP Access Driver【每日一译】--20121225
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- user master page with asp:FormParameter , save only blank data to Database MSAccess
- Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been close
- 75.Your test database is configured to run in NOARCHIVELOG mode. One of the data files in the USERS
- media foundation (Using the Source Reader to Process Media Data)
- Unable to Drop a Datafile From the Tablespace Using Alter Tablespace Command (Doc ID 1050261.1)
- hadoop集群,突然断电后再次登录集群发现主结点连接各个datanode结点时出现:Agent admitted failure to sign using the key
- How to read and save Images in a Sql Server Database using ADO.NET and C#