Csharp/C Sharp/Database ADO.net/OleDbConnection — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:42, 26 мая 2010

OleDbConnection connection: ConnectionString, Database, DataSource, ServerVersion and State

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: ConnectionString property

 
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;" +     
                @"Data Source=.\sqlexpress;" + 
                "Initial Catalog=Northwind;" + 
                "Integrated Security=SSPI";    
            con.Open();
            if (con.State == ConnectionState.Open) {
                Console.WriteLine("  Connection String = " +
                    con.ConnectionString);
            } else {
                Console.WriteLine("OleDbConnection failed to open.");
                Console.WriteLine("  Connection State = " + con.State);
            }
        }
    }
}


OleDbConnection: Database

 
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;" +     
                @"Data Source=.\sqlexpress;" + 
                "Initial Catalog=Northwind;" + 
                "Integrated Security=SSPI";    
            con.Open();
            if (con.State == ConnectionState.Open) {
                Console.WriteLine("  Database = " + con.Database);
            } else {
                Console.WriteLine("OleDbConnection failed to open.");
                Console.WriteLine("  Connection State = " + con.State);
            }
        }
    }
}


OleDbConnection: DataSource

 
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;" +     
                @"Data Source=.\sqlexpress;" + 
                "Initial Catalog=Northwind;" + 
                "Integrated Security=SSPI";    
            con.Open();
            if (con.State == ConnectionState.Open) {
                Console.WriteLine("  Database Source = " + con.DataSource);
            } else {
                Console.WriteLine("OleDbConnection failed to open.");
                Console.WriteLine("  Connection State = " + con.State);
            }
        }
    }
}


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;" +     
                @"Data Source=.\sqlexpress;" + 
                "Initial Catalog=Northwind;" + 
                "Integrated Security=SSPI";    
            con.Open();
            if (con.State == ConnectionState.Open) {
                Console.WriteLine("  Server Version = " + con.ServerVersion);
            } else {
                Console.WriteLine("OleDbConnection failed to open.");
                Console.WriteLine("  Connection State = " + con.State);
            }
        }
    }
}


OleDbConnection: State

 
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;" +     
                @"Data Source=.\sqlexpress;" + 
                "Initial Catalog=Northwind;" + 
                "Integrated Security=SSPI";    
            con.Open();
            if (con.State == ConnectionState.Open) {
                Console.WriteLine("  Connection State = " + con.State);
            } else {
                Console.WriteLine("OleDbConnection failed to open.");
                Console.WriteLine("  Connection State = " + con.State);
            }
        }
    }
}


OleDbConnection string

 
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
public class MainClass {
    static void Main() {
        try {
            OleDbConnection cnOleDb = new OleDbConnection("Provider=SQLOLEDB.1;Password=pw;Persist Security Info=False;User ID=admin;Initial Catalog=biblio;Data Source=betav9;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=BETAV9;Use Encryption for Data=False;Tag with column collation when possible=False");
            cnOleDb.Open();
        } catch (OleDbException exOleDb) {
            MessageBox.Show(exOleDb.Message);
        } catch (Exception ex) {
            MessageBox.Show(ex.Message); 
        }
    }
}


Read data from ole db connection

using System;
using System.Data.OleDb;
public class ReadFromOleDb
{
  [STAThread]
  static void Main(string[] args)
  {
    String sConn = "provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    
    String sSQL = "select id, firstname, lastname from Employee";
    OleDbConnection oConn = new OleDbConnection(sConn);
    oConn.Open();
    OleDbCommand oCmd = new OleDbCommand(sSQL, oConn);
    OleDbDataReader oReader = oCmd.ExecuteReader();
    int idxID = oReader.GetOrdinal("id");
    int idxFirstName = oReader.GetOrdinal("firstname");
    int idxLastName = oReader.GetOrdinal("lastname");
    while(oReader.Read()) {
      Console.WriteLine("{0} {1} {2}",
        oReader.GetValue(idxID),
        oReader.GetValue(idxFirstName),
        oReader.GetValue(idxLastName));
    }
  }
}