Csharp/C Sharp by API/System.Data.OleDb/OleDbConnection — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:10, 26 мая 2010
Содержание
new OleDbConnection
using System;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
OleDbConnection MyConnection = new OleDbConnection (@"Provider=Microsft.Jet.OLEDB.4.0; Data Source = c:\Northwind.mdb");
MyConnection.Open();
}
}
OleDbConnection.ConnectionString
using System;
using System.Data;
using System.Data.OleDb;
class AccessConnect {
static void Main()
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
Console.WriteLine("Connection opened.");
Console.WriteLine("Connection Properties:");
Console.WriteLine("\tConnection String: {0}", conn.ConnectionString);
Console.WriteLine("\tDatabase: {0}",conn.Database);
Console.WriteLine("\tDataSource: {0}",conn.DataSource);
Console.WriteLine("\tServerVersion: {0}", conn.ServerVersion);
Console.WriteLine("\tState: {0}", conn.State);
} catch (OleDbException e) {
Console.WriteLine("Error: " + e);
} finally {
conn.Close();
Console.WriteLine("Connection closed.");
}
}
}
OleDbConnection.CreateCommand
using System;
using System.Data;
using System.Data.OleDb;
class OleDbConnectionAccess {
public static void Main() {
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=F:\\Program Files\\MicrosoftOffice\\Office\\Samples\\Northwind.mdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbCommand.rumandText = "SELECT CustomerID, CompanyName, ContactName, Address FROM Customers WHERE CustomerID = "ALFKI"";
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
Console.WriteLine("myOleDbDataReader[\" CustomerID\"] = " + myOleDbDataReader["CustomerID"]);
Console.WriteLine("myOleDbDataReader[\" CompanyName\"] = " + myOleDbDataReader["CompanyName"]);
Console.WriteLine("myOleDbDataReader[\" ContactName\"] = " + myOleDbDataReader["ContactName"]);
Console.WriteLine("myOleDbDataReader[\" Address\"] = " + myOleDbDataReader["Address"]);
myOleDbDataReader.Close();
myOleDbConnection.Close();
}
}
OleDbConnection.Database
using System;
using System.Data;
using System.Data.OleDb;
class AccessConnect {
static void Main()
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
Console.WriteLine("Connection opened.");
Console.WriteLine("Connection Properties:");
Console.WriteLine("\tConnection String: {0}", conn.ConnectionString);
Console.WriteLine("\tDatabase: {0}",conn.Database);
Console.WriteLine("\tDataSource: {0}",conn.DataSource);
Console.WriteLine("\tServerVersion: {0}", conn.ServerVersion);
Console.WriteLine("\tState: {0}", conn.State);
} catch (OleDbException e) {
Console.WriteLine("Error: " + e);
} finally {
conn.Close();
Console.WriteLine("Connection closed.");
}
}
}
OleDbConnection.DataSource
using System;
using System.Data;
using System.Data.OleDb;
class AccessConnect {
static void Main()
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
Console.WriteLine("Connection opened.");
Console.WriteLine("Connection Properties:");
Console.WriteLine("\tConnection String: {0}", conn.ConnectionString);
Console.WriteLine("\tDatabase: {0}",conn.Database);
Console.WriteLine("\tDataSource: {0}",conn.DataSource);
Console.WriteLine("\tServerVersion: {0}", conn.ServerVersion);
Console.WriteLine("\tState: {0}", conn.State);
} catch (OleDbException e) {
Console.WriteLine("Error: " + e);
} finally {
conn.Close();
Console.WriteLine("Connection closed.");
}
}
}
OleDbConnection.GetOleDbSchemaTable
using System;
using System.Data;
using System.Data.OleDb;
public class DatabaseInfo {
public static void Main () {
String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
OleDbConnection con = new OleDbConnection(connect);
con.Open();
Console.WriteLine("Made the connection to the database");
Console.WriteLine("Information for each table contains:");
DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
DataTable cols = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new object[]{null,null,"Employee",null});
Console.WriteLine("The columns in the Customer table are:");
foreach(DataRow row in cols.Rows)
Console.WriteLine(" {0}\t{1}", row[3],(OleDbType)row[11]);
con.Close();
}
}
OleDbConnection.ReleaseObjectPool()
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
class MainClass
{
static void Main(string[] args)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();
conn.Close();
OleDbConnection.ReleaseObjectPool();
}
}
OleDbConnection.ServerVersion
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
class MainClass
{
public static void Main()
{
using (OleDbConnection con = new OleDbConnection())
{
con.ConnectionString ="provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
con.Open();
if (con.State == ConnectionState.Open)
{
Console.WriteLine("OleDbConnection Information:");
Console.WriteLine(" Connection State = " + con.State);
Console.WriteLine(" Connection String = " +
con.ConnectionString);
Console.WriteLine(" Database Source = " + con.DataSource);
Console.WriteLine(" Database = " + con.Database);
Console.WriteLine(" Server Version = " + con.ServerVersion);
Console.WriteLine(" Timeout = " + con.ConnectionTimeout);
}
else
{
Console.WriteLine("OleDbConnection failed to open.");
Console.WriteLine(" Connection State = " + con.State);
}
}
}
}
OleDbConnection.State
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
public class MainClass{
static void Main() {
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
OleDbConnection conn = new OleDbConnection(ConnectionString);
// Open the connection
if( conn.State != ConnectionState.Open)
conn.Open();
// Show the connection properties
Console.WriteLine( "Connection String :"+conn.ConnectionString
+", DataSource :"+ conn.DataSource.ToString()
+", Provider :"+ conn.Provider.ToString()
+", Server Version:"+ conn.ServerVersion.ToString()
+", Connection Time Out:"+ conn.ConnectionTimeout.ToString() );
// Close the connection
if( conn.State == ConnectionState.Open)
conn.Close();
}
}