Csharp/CSharp Tutorial/ADO.Net/OdbcConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
OdbcConnection for Access mdb file
using System;
using Microsoft.Data.Odbc;
class MainClass
{
static void Main(string[] args)
{
string connectionString = @"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\Northwind.mdb";
string SQL = "SELECT * FROM Orders";
OdbcConnection conn = new OdbcConnection(connectionString);
OdbcCommand cmd = new OdbcCommand(SQL);
cmd.Connection = conn;
conn.Open();
OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.Write("OrderID:"+reader.GetInt32(0).ToString() );
Console.Write(" ,");
Console.WriteLine("Customer:" + reader.GetString(1).ToString() );
}
reader.Close();
conn.Close();
}
}
Use ODBC Data Provider
using System;
using System.Data;
using System.Data.Odbc;
class MainClass
{
static void Main(string[] args)
{
string connString = @"dsn=northwindodbc";
string sql = @"select * from employee";
OdbcConnection conn = null;
OdbcDataReader reader = null;
try
{
conn = new OdbcConnection(connString);
conn.Open();
OdbcCommand cmd = new OdbcCommand(sql, conn);
reader = cmd.ExecuteReader();
Console.WriteLine("Querying database {0} with query {1}\n", conn.Database, cmd.rumandText );
while(reader.Read()) {
Console.WriteLine("{0} | {1}", reader["FirstName"].ToString().PadLeft(10) , reader[1].ToString().PadLeft(10) );
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
reader.Close();
conn.Close();
}
}
}