急用抄过来不整理了-Access
using System;
using System.Data;
using System.Data.OleDb;
class TestADO
{
static void Main(string[] args)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb";
string strSQL = "SELECT * FROM employees" ;
OleDbConnection conn = new OleDbConnection(strDSN);
OleDbCommand cmd = new OleDbCommand( strSQL, conn );
OleDbDataReader reader = null;
try
{
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read() )
{
Console.WriteLine("First Name:{0}, Last Name:{1}", reader["FirstName"], reader["LastName"]);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
conn.Close();
}
}
}
using System;
using System.Data;
using System.Data.OleDb;
class TestADO
{
static void Main(string[] args)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";
string strSQL = "INSERT INTO Employee(FirstName, LastName) VALUES('FirstName', 'LastName')" ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection conn = new OleDbConnection(strDSN);
OleDbCommand cmd = new OleDbCommand( strSQL, conn );
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Oooops. I did it again:\n{0}", e.Message);
}
finally
{
conn.Close();
}
}
}
十二、使用OLEConn连接数据库:
using System;
using System.Data;
using System.Data.OleDb;
class TestADO
{
static void Main(string[] args)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";
string strSQL = "SELECT * FROM employee" ;
OleDbConnection conn = new OleDbConnection(strDSN);
OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
conn.Open();
DataSet ds = new DataSet();
cmd.Fill( ds, "employee" );
DataTable dt = ds.Tables[0];
foreach( DataRow dr in dt.Rows )
{
Console.WriteLine("First name: "+ dr["FirstName"].ToString() + " Last name: "+ dr["LastName"].ToString());
}
conn.Close();
}
}
十三、读取表的属性:
using System;
using System.Data;
using System.Data.OleDb;
class TestADO
{
static void Main(string[] args)
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";
string strSQL = "SELECT * FROM employee" ;
OleDbConnection conn = new OleDbConnection(strDSN);
OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
conn.Open();
DataSet ds = new DataSet();
cmd.Fill( ds, "employee" );
DataTable dt = ds.Tables[0];
Console.WriteLine("Field Name DataType Unique AutoIncrement AllowNull");
Console.WriteLine("==================================================================");
foreach( DataColumn dc in dt.Columns )
{
Console.WriteLine(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
}
conn.Close();
}
}