一、连接Access数据库:
二、连接SQL Server数据库:
三、从SQL内读数据到XML:
四、用ADO添加数据到数据库中:
五、使用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;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.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();
}
}
}
二、连接SQL Server数据库:
using
System;
using System.Data.SqlClient;
public class TestADO
... {
public static void Main()
...{
SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
SqlCommand cmd = new SqlCommand(SELECT * FROM employees, conn);
try
...{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
...{
Console.WriteLine(First Name: ...{0}, Last Name: ...{1}, reader.GetString(0), reader.GetString(1));
}
reader.Close();
conn.Close();
}
catch(Exception e)
...{
Console.WriteLine(Exception Occured -->> ...{0},e);
}
}
}
using System.Data.SqlClient;
public class TestADO
... {
public static void Main()
...{
SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
SqlCommand cmd = new SqlCommand(SELECT * FROM employees, conn);
try
...{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
...{
Console.WriteLine(First Name: ...{0}, Last Name: ...{1}, reader.GetString(0), reader.GetString(1));
}
reader.Close();
conn.Close();
}
catch(Exception e)
...{
Console.WriteLine(Exception Occured -->> ...{0},e);
}
}
}
三、从SQL内读数据到XML:
using
System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
public class TestWriteXML
... {
public static void Main()
...{
String strFileName=c:/temp/output.xml;
SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
String strSql = SELECT FirstName, LastName FROM employees;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(strSql,conn);
// Build the DataSet
DataSet ds = new DataSet();
adapter.Fill(ds, employees);
// Get a FileStream object
FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);
// Apply the WriteXml method to write an XML document
ds.WriteXml(fs);
fs.Close();
}
}
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
public class TestWriteXML
... {
public static void Main()
...{
String strFileName=c:/temp/output.xml;
SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
String strSql = SELECT FirstName, LastName FROM employees;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(strSql,conn);
// Build the DataSet
DataSet ds = new DataSet();
adapter.Fill(ds, employees);
// Get a FileStream object
FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);
// Apply the WriteXml method to write an XML document
ds.WriteXml(fs);
fs.Close();
}
}
四、用ADO添加数据到数据库中:
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: est.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: ...{0}, e.Message);
}
finally
...{
conn.Close();
}
}
}
using System.Data;
using System.Data.OleDb;
class TestADO
... {
static void Main(string[] args)
...{
string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c: est.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: ...{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: est.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.Data;
using System.Data.OleDb;
class TestADO
... {
static void Main(string[] args)
...{
string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c: est.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: est.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();
}
}
using System.Data;
using System.Data.OleDb;
class TestADO
... {
static void Main(string[] args)
...{
string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c: est.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();
}
}