Sie sind auf Seite 1von 22

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(); } } 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(); } }

using System; using System.Data; using System.Data.Common; using System.Data.OleDb; class MainClass { static void Main(string[] args) { string connectionString = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\Northwind.m db"; 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();

} }

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.CommandText = "SELECT CustomerID, CompanyName, ContactName, Address FROM Customers WHERE CustomerID = 'ALFKI'"; myOleDbConnection.Open(); OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); myOleDbDataReader.Read(); Console.WriteLine("myOleDbDataReader[\" omerID"]); Console.WriteLine("myOleDbDataReader[\" panyName"]); Console.WriteLine("myOleDbDataReader[\" tactName"]); Console.WriteLine("myOleDbDataReader[\" "]); myOleDbDataReader.Close(); myOleDbConnection.Close(); CustomerID\"] = " + myOleDbDataReader["Cust CompanyName\"] = " + myOleDbDataReader["Com ContactName\"] = " + myOleDbDataReader["Con Address\"] = " + myOleDbDataReader["Address

} }

Output properties for OleDbConnection


using using using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb; 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(); } }

27. 5. 2. OleDbConnection connection state


using using using using System; System.Data; System.Data.SqlClient; System.Data.OleDb;

class MainClass { public static void Main() { using (OleDbConnection con = new OleDbConnection()) { con.ConnectionString ="provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDat abase;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); } } }

27. 5. 3. Connection Pooling Sample


using using using using System; System.Data; System.Data.SqlClient; 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();

} }

27. 6. 1. OleDbCommand: Execute Scalar


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

27. 6. 2. Connect Table to OleDbCommand using CommandType.TableDirect

using using using using

System; System.Data; System.Data.OleDb; 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); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; cmd.CommandText = "Customers"; cmd.CommandType = CommandType.TableDirect; conn.Open(); OleDbDataReader reader = cmd.ExecuteReader(); Console.WriteLine("Customer Id, Contact Name, Company Name"); while (reader.Read()) { Console.Write(reader["CustomerID"].ToString()); Console.Write(", "+ reader["ContactName"].ToString()); Console.WriteLine(", "+ reader["CompanyName"].ToString()); } reader.Close(); conn.Close(); }

27. 6. 3. Execute Non Query


using using using using System; System.Data; System.Data.OleDb; 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.CommandText = @"DELETE * FROM Customers"; cmd.ExecuteNonQuery(); conn.Close();

} }

27. 6. 4. Use OLE DB Data Provider


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;In tegrated 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.Comman

dText ); 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(); } } }

27. 7. 1. OleDbParameter Example


using using using using using System; System.Drawing; System.Collections; System.Data; System.Data.OleDb;

class MainClass{

[STAThread] static void Main() { string cstr = "provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Secu using ( OleDbConnection conn = new OleDbConnection( cstr ) ) { conn.Open(); string selstr = "select firstName from Employee where lastname = ?"; OleDbCommand cmd = new OleDbCommand( selstr, conn ); OleDbParameter name = cmd.Parameters.Add( "@name", OleDbType.VarChar, 15 ); name.Value = "Tang"; OleDbDataReader rdr = cmd.ExecuteReader(); if ( rdr.Read() ) { Console.WriteLine(rdr.GetString( 0 ) ); } else { Console.WriteLine(" is not available yet" ); }

} } }

27. 8. 1. Insert Data and reload to DataGrid

using using using using using using using using

System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb; System.Data.SqlClient;

public class InsertDataLoadDataGrid : System.Windows.Forms.Form {

private private private private private

System.Windows.Forms.DataGrid dataGrid1; System.Windows.Forms.Button InsertCommand; System.Windows.Forms.Button UpdateCommand; System.Windows.Forms.Button DeleteCommand; System.Windows.Forms.CheckBox checkBox1;

private System.ComponentModel.Container components = null; public InsertDataLoadDataGrid() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.InsertCommand = new System.Windows.Forms.Button(); this.UpdateCommand = new System.Windows.Forms.Button(); this.DeleteCommand = new System.Windows.Forms.Button(); this.checkBox1 = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(336, 304); this.dataGrid1.TabIndex = 0; // // InsertCommand // this.InsertCommand.Location = new System.Drawing.Point(368, 24); this.InsertCommand.Name = "InsertCommand"; this.InsertCommand.Size = new System.Drawing.Size(120, 32); this.InsertCommand.TabIndex = 1; this.InsertCommand.Text = "Insert Command"; this.InsertCommand.Click += new System.EventHandler(this.InsertCommand_Click); // // UpdateCommand // this.UpdateCommand.Location = new System.Drawing.Point(368, 72); this.UpdateCommand.Name = "UpdateCommand"; this.UpdateCommand.Size = new System.Drawing.Size(120, 32); this.UpdateCommand.TabIndex = 2; this.UpdateCommand.Text = "Update Command"; this.UpdateCommand.Click += new System.EventHandler(this.UpdateCommand_Click); // // DeleteCommand //

this.DeleteCommand.Location = new System.Drawing.Point(368, 120); this.DeleteCommand.Name = "DeleteCommand"; this.DeleteCommand.Size = new System.Drawing.Size(120, 32); this.DeleteCommand.TabIndex = 3; this.DeleteCommand.Text = "Delete Command"; this.DeleteCommand.Click += new System.EventHandler(this.DeleteCommand_Click); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(368, 192); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(112, 24); this.checkBox1.TabIndex = 4; this.checkBox1.Text = "SqlCommand"; // // InsertDataLoadDataGrid // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(496, 325); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.checkBox1, this.DeleteCommand, this.UpdateCommand, this.InsertCommand, this.dataGrid1}); this.Name = "InsertDataLoadDataGrid"; this.Text = "InsertDataLoadDataGrid"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new InsertDataLoadDataGrid()); }

private void InsertCommand_Click(object sender, System.EventArgs e) { string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Northwind.mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); DataRow row; DataSet ds = new DataSet(); try { conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * FROM Customers", conn); OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; adapter.Fill(ds, "Customers"); row = ds.Tables["Customers"].NewRow(); row["CustomerId"] = "001"; row["ContactName"] = "L"; row["CompanyName"] = "M"; ds.Tables["Customers"].Rows.Add(row); adapter.Update(ds, "Customers");

dataGrid1.DataSource = ds.DefaultViewManager; } catch(OleDbException exp) { MessageBox.Show(exp.Message.ToString()); } if(conn.State == ConnectionState.Open) conn.Close(); } private void UpdateCommand_Click(object sender, System.EventArgs e) { } private void DeleteCommand_Click(object sender, System.EventArgs e) { }

27. 8. 2. Update data and reload to DataGrid

using using using using using using using using

System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb; System.Data.SqlClient;

public class UpdateDataLoadDataGrid : System.Windows.Forms.Form {

private private private private private

System.Windows.Forms.DataGrid dataGrid1; System.Windows.Forms.Button InsertCommand; System.Windows.Forms.Button UpdateCommand; System.Windows.Forms.Button DeleteCommand; System.Windows.Forms.CheckBox checkBox1;

private System.ComponentModel.Container components = null; public UpdateDataLoadDataGrid() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.InsertCommand = new System.Windows.Forms.Button(); this.UpdateCommand = new System.Windows.Forms.Button(); this.DeleteCommand = new System.Windows.Forms.Button(); this.checkBox1 = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(336, 304); this.dataGrid1.TabIndex = 0; // // InsertCommand // this.InsertCommand.Location = new System.Drawing.Point(368, 24); this.InsertCommand.Name = "InsertCommand"; this.InsertCommand.Size = new System.Drawing.Size(120, 32); this.InsertCommand.TabIndex = 1; this.InsertCommand.Text = "Insert Command"; this.InsertCommand.Click += new System.EventHandler(this.InsertCommand_Click); // // UpdateCommand // this.UpdateCommand.Location = new System.Drawing.Point(368, 72); this.UpdateCommand.Name = "UpdateCommand"; this.UpdateCommand.Size = new System.Drawing.Size(120, 32); this.UpdateCommand.TabIndex = 2; this.UpdateCommand.Text = "Update Command"; this.UpdateCommand.Click += new System.EventHandler(this.UpdateCommand_Click); // // DeleteCommand //

this.DeleteCommand.Location = new System.Drawing.Point(368, 120); this.DeleteCommand.Name = "DeleteCommand"; this.DeleteCommand.Size = new System.Drawing.Size(120, 32); this.DeleteCommand.TabIndex = 3; this.DeleteCommand.Text = "Delete Command"; this.DeleteCommand.Click += new System.EventHandler(this.DeleteCommand_Click); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(368, 192); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(112, 24); this.checkBox1.TabIndex = 4; this.checkBox1.Text = "SqlCommand"; // // UpdateDataLoadDataGrid // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(496, 325); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.checkBox1, this.DeleteCommand, this.UpdateCommand, this.InsertCommand, this.dataGrid1}); this.Name = "UpdateDataLoadDataGrid"; this.Text = "UpdateDataLoadDataGrid"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new UpdateDataLoadDataGrid()); } private void InsertCommand_Click(object sender, System.EventArgs e) { } private void UpdateCommand_Click(object sender, System.EventArgs e) { string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source=c:\\Northwind.mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); DataSet ds = new DataSet(); try { conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * FROM Customers", conn); OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; adapter.Fill(ds, "Customers"); DataRow row1 = ds.Tables["Customers"].Rows.Find("001"); row1["ContactName"]="S";

row1["CompanyName"] = "M"; adapter.Update(ds, "Customers"); dataGrid1.DataSource = ds.DefaultViewManager;

} catch(OleDbException exp) { MessageBox.Show(exp.Message.ToString()); } if(conn.State == ConnectionState.Open) conn.Close();

private void DeleteCommand_Click(object sender, System.EventArgs e) { } }

27. 8. 3. Delete data and reload to DataGrid

using using using using using using using using

System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb; System.Data.SqlClient;

public class DeleteDataLoadDataGrid : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.Button InsertCommand; private System.Windows.Forms.Button UpdateCommand; private System.Windows.Forms.Button DeleteCommand;

private System.Windows.Forms.CheckBox checkBox1; private System.ComponentModel.Container components = null; public DeleteDataLoadDataGrid() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.InsertCommand = new System.Windows.Forms.Button(); this.UpdateCommand = new System.Windows.Forms.Button(); this.DeleteCommand = new System.Windows.Forms.Button(); this.checkBox1 = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8, 8); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(336, 304); this.dataGrid1.TabIndex = 0; // // InsertCommand // this.InsertCommand.Location = new System.Drawing.Point(368, 24); this.InsertCommand.Name = "InsertCommand"; this.InsertCommand.Size = new System.Drawing.Size(120, 32); this.InsertCommand.TabIndex = 1; this.InsertCommand.Text = "Insert Command"; this.InsertCommand.Click += new System.EventHandler(this.InsertCommand_Click); // // UpdateCommand // this.UpdateCommand.Location = new System.Drawing.Point(368, 72); this.UpdateCommand.Name = "UpdateCommand"; this.UpdateCommand.Size = new System.Drawing.Size(120, 32); this.UpdateCommand.TabIndex = 2; this.UpdateCommand.Text = "Update Command"; this.UpdateCommand.Click += new System.EventHandler(this.UpdateCommand_Click); // // DeleteCommand // this.DeleteCommand.Location = new System.Drawing.Point(368, 120); this.DeleteCommand.Name = "DeleteCommand"; this.DeleteCommand.Size = new System.Drawing.Size(120, 32); this.DeleteCommand.TabIndex = 3;

this.DeleteCommand.Text = "Delete Command"; this.DeleteCommand.Click += new System.EventHandler(this.DeleteCommand_Click); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(368, 192); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(112, 24); this.checkBox1.TabIndex = 4; this.checkBox1.Text = "SqlCommand"; // // DeleteDataLoadDataGrid // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(496, 325); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.checkBox1, this.DeleteCommand, this.UpdateCommand, this.InsertCommand, this.dataGrid1}); this.Name = "DeleteDataLoadDataGrid"; this.Text = "DeleteDataLoadDataGrid"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new DeleteDataLoadDataGrid()); } private void InsertCommand_Click(object sender, System.EventArgs e) { } private void UpdateCommand_Click(object sender, System.EventArgs e) { } private void DeleteCommand_Click(object sender, System.EventArgs e) { string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source=c:\\Northwind.mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); DataSet ds = new DataSet(); try { conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * FROM Customers", conn); OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; adapter.Fill(ds, "Customers");

DataRow row1 = ds.Tables["Customers"].Rows.Find("001"); row1.Delete(); adapter.Update(ds, "Customers"); dataGrid1.DataSource = ds.DefaultViewManager; } catch(OleDbException exp) { MessageBox.Show(exp.Message.ToString()); } if(conn.State == ConnectionState.Open) conn.Close(); }

27. 9. 1. Updating a DataSource


using System; using System.Data; using System.Data.OleDb;

class MainClass { static void Main(string[] args) { OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Sou OleDbDataAdapter MyAdapter = new OleDbDataAdapter("SELECT * FROM orders", MyConnection); DataSet MyDataSet = new DataSet(); MyAdapter.Fill(MyDataSet, "orders"); MyDataSet.Tables[0].Rows[1]["ID"] = "1"; OleDbCommandBuilder MyBuilder = new OleDbCommandBuilder(MyAdapter); } } MyAdapter.Update(MyDataSet.Tables[0]);

27. 9. 2. Using MultiTabled Datasets


using System; using System.Data; using System.Data.OleDb;

class MainClass { static void Main(string[] args) { OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Sou OleDbDataAdapter MyAdapter = new OleDbDataAdapter("SELECT * FROM Orders", MyConnection); DataSet MyDataSet = new DataSet(); MyAdapter.Fill(MyDataSet, "Orders"); foreach (DataTable MyTable in MyDataSet.Tables) { foreach (DataColumn MyColumn in MyTable.Columns) { foreach (DataRow MyRow in MyTable.Rows)

{ } } } } }

27. 10. 1. Read result set from OleDbDataReader


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;In tegrated 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.Comman

dText ); 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(); } } }

27. 10. 2. Use to OleDbDataReader retrieve data


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(); } }

27. 10. 3. OleDbDataAdapter and SqlDataAdapter: Update

using System; using System.Drawing;

using using using using using using

System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.SqlClient; System.Data.OleDb;

public class OleDbDataAdapterSqlDataAdapter : System.Windows.Forms.Form { private System.Windows.Forms.Button OleDbDataAdapter; private System.Windows.Forms.Button SqlDataAdapter; private System.Windows.Forms.DataGrid dataGrid1; private System.ComponentModel.Container components = null; public OleDbDataAdapterSqlDataAdapter() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.OleDbDataAdapter = new System.Windows.Forms.Button(); this.SqlDataAdapter = new System.Windows.Forms.Button(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // OleDbDataAdapter // this.OleDbDataAdapter.Location = new System.Drawing.Point(16, 16); this.OleDbDataAdapter.Name = "OleDbDataAdapter"; this.OleDbDataAdapter.Size = new System.Drawing.Size(136, 32); this.OleDbDataAdapter.TabIndex = 0; this.OleDbDataAdapter.Text = "OleDb DataAdapter"; this.OleDbDataAdapter.Click += new System.EventHandler(this.OleDbDataAdapter_Click); // // SqlDataAdapter // this.SqlDataAdapter.Location = new System.Drawing.Point(176, 16); this.SqlDataAdapter.Name = "SqlDataAdapter"; this.SqlDataAdapter.Size = new System.Drawing.Size(168, 32); this.SqlDataAdapter.TabIndex = 1; this.SqlDataAdapter.Text = "SQL DataAdapter"; this.SqlDataAdapter.Click += new System.EventHandler(this.SqlDataAdapter_Click); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(16, 72); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(408, 192);

this.dataGrid1.TabIndex = 2; // // OleDbDataAdapterSqlDataAdapter // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(440, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1, this.SqlDataAdapter, this.OleDbDataAdapter}); this.Name = "OleDbDataAdapterSqlDataAdapter"; this.Text = "OleDbDataAdapterSqlDataAdapter"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new OleDbDataAdapterSqlDataAdapter()); } private void OleDbDataAdapter_Click(object sender, System.EventArgs e) { string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind .mdb"; string SQL = "SELECT * FROM Customers"; OleDbConnection conn = new OleDbConnection(ConnectionString); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand(SQL, conn); DataSet ds = new DataSet("Customers"); adapter.Fill(ds, "Customers"); adapter.DeleteCommand = new OleDbCommand("DELETE * FROM Customers"); adapter.Update(ds, "Customers"); dataGrid1.DataSource = ds.DefaultViewManager; } private void SqlDataAdapter_Click(object sender, System.EventArgs e) { string ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=Northwind;" + "Data Source=localhost;"; string SQL = "SELECT CustomerID, CompanyName FROM Customers"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter(SQL, conn); DataSet ds = new DataSet("Customers"); adapter.Fill(ds); } } dataGrid1.DataSource = ds.DefaultViewManager;

27. 11. 1. Deal with OleDbException


using using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb;

public class MainClass { public static void Main(){ string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind .mdb"; OleDbConnection conn = new OleDbConnection(ConnectionString); try { conn.Open(); } catch(OleDbException ae) { for (int i = 0; i < ae.Errors.Count; i++) { Console.WriteLine(ae.Errors[i].Message + " - " + ae.Errors[i].SQLState); } } } } 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.Com mandText ); while(reader.Read()) { Console.WriteLine("{0} | {1}", reader["FirstName"].ToString().PadLeft(10) , rea der[1].ToString().PadLeft(10) ); } } catch (Exception e)

Console.WriteLine("Error: " + e); } finally { reader.Close(); conn.Close(); } } }

Das könnte Ihnen auch gefallen