C# 数据库读取和写入
/下面为数据库数据的读取
using System.Data.OleDb
string strDSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=meTest.MDB";
string strSQL="SELECT * FROM Developer";
//实例化OleDbConnection对象
OleDbConnection myConn=new OleDbConnection(strDSN);
//实例化OleDbCommand对象
OleDbCommand myCmd = new OleDbCommand(strSQL,myConn);
//实例化OleDbDataReader对象
OleDbDataReader datareader = null;
//链接数据库,读取数据
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
while(datareader.Read())
{
//datareader["name"],datareader["address"];
}
}
catch(Exception e)
{
}
finally
{
myConn.Close();
}
//下面为对数据库的表单进行数据的写入
using System.Data.OleDb;
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=meTest.MDB";
string strSQL = "INSERT INTO Developer(Name, Address)VALUES
('NewName','NewAddress')";
//实例化OleDbCommand对象
OleDbConnection myConn = new OleDbConnection(strDSN);
//实例化OleDbCommand对象
OleDbCommand myCmd = new OleDbCommand(strSQL,myConn);
try
{
myConn.Open();
myCmd.ExecuteNinQuery();
}
catch(Exception e)
{
}
finally
{
myConn.Close();
}
//从DataSet中读取数据库数据
//读取列的属性