Csharp/C Sharp by API/System.Data.OleDb/OleDbDataReader
Версия от 15:31, 26 мая 2010; (обсуждение)
OleDbDataReader.GetInt32
using System;
using System.Data;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
string SQL = "SELECT * FROM Orders";
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand(SQL);
cmd.Connection = conn;
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
Console.Write("OrderID:"+reader.GetInt32(0).ToString() );
Console.Write(" ,");
Console.WriteLine("Customer:" + reader.GetString(1).ToString() );
}
}
finally
{
reader.Close();
conn.Close();
}
}
}
OleDbDataReader.GetString
using System;
using System.Data;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
string SQL = "SELECT * FROM Orders";
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand(SQL);
cmd.Connection = conn;
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
Console.Write("OrderID:"+reader.GetInt32(0).ToString() );
Console.Write(" ,");
Console.WriteLine("Customer:" + reader.GetString(1).ToString() );
}
}
finally
{
reader.Close();
conn.Close();
}
}
}
OleDbDataReader.Read()
using System;
using System.Data;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
string connString = "provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select * from employee";
OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand(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();
}
}
}