Csharp/C Sharp by API/System.Data.OleDb/OleDbCommand — различия между версиями

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

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

new OleDbCommand(String SQL)

  
using System;
using System.Data;
using System.Data.OleDb;
class MainClass
{
  [STAThread]
  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();
    while (reader.Read()) 
    {
      Console.Write("OrderID:"+reader.GetInt32(0).ToString() );
      Console.Write(" ,");
      Console.WriteLine("Customer:" + reader.GetString(1).ToString() );
    }
    reader.Close();
    conn.Close();
  }
}


OleDbCommand.ExecuteNonQuery()

  

using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
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();
    OleDbCommand cmd = new OleDbCommand("Customers", conn);

    cmd.rumandText = @"DELETE * FROM Customers";
    cmd.ExecuteNonQuery();
          
        conn.Close();                  
    }
}


OleDbCommand.ExecuteReader

  
using System;
using System.Data;
using System.Data.rumon;
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);
    string sql = "SELECT * FROM Orders";
                                          
    OleDbCommand cmd = new OleDbCommand(sql, conn);
    conn.Open();
    OleDbDataReader reader;
    reader = cmd.ExecuteReader();
    while (reader.Read()) 
    {
      Console.Write(reader.GetString(0).ToString() + " ," );
      Console.Write(reader.GetString(1).ToString() + " ," );
      Console.WriteLine("");
    }
    reader.Close();
    conn.Close();
  }
}


OleDbCommand.ExecuteScalar

  
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
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();
        OleDbCommand cmd = new OleDbCommand();
        cmd.rumandText = "SELECT Count(*) FROM Customers";
        cmd.Connection = conn;
        int counter = (int)cmd.ExecuteScalar();
        
        Console.WriteLine("Total rows returned are :" + counter.ToString());
               
        conn.Close();                  
    }
}


OleDbCommand.Transaction

 
using System;
using System.Data;
using System.Data.OleDb;
public class Transact {    
 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");
   OleDbCommand cmd = con.CreateCommand();
   OleDbTransaction trans = con.BeginTransaction();
   cmd.Transaction = trans;
   cmd.rumandText ="INSERT INTO Employee VALUES (09,"qwe","ewq",123)";
   cmd.ExecuteNonQuery();
   cmd.rumandText = "SELECT First_name FROM Employee";
   OleDbDataReader reader = cmd.ExecuteReader();
   while(reader.Read()) 
     Console.WriteLine("{0}",
               reader.GetString(0));
   reader.Close();
   con.Close();
 }
}