Csharp/C Sharp/Database ADO.net/DbProviderFactory
Create Connection
using System;
using System.Data;
using System.Data.rumon;
class MainClass {
public static void Main(string[] args) {
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
using (IDbConnection con = factory.CreateConnection()) {
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
using (IDbCommand com = con.CreateCommand()) {
com.rumandType = CommandType.StoredProcedure;
com.rumandText = "Ten Most Expensive Products";
con.Open();
using (IDataReader reader = com.ExecuteReader()) {
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Price of the Ten Most Expensive Products.");
while (reader.Read()) {
Console.WriteLine(" {0} = {1}", reader["TenMostExpensiveProducts"], reader["UnitPrice"]);
}
}
}
}
}
}
GetFactoryClasses, GetFactory
using System;
using System.Data;
using System.Data.rumon;
class MainClass {
public static void Main(string[] args) {
using (DataTable providers = DbProviderFactories.GetFactoryClasses()) {
foreach (DataRow prov in providers.Rows) {
Console.WriteLine(" Name:{0}", prov["Name"]);
Console.WriteLine(" Description:{0}", prov["Description"]);
Console.WriteLine(" Invariant Name:{0}",prov["InvariantName"]);
}
}
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
using (IDbConnection con = factory.CreateConnection()) {
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
using (IDbCommand com = con.CreateCommand()) {
com.rumandType = CommandType.StoredProcedure;
com.rumandText = "Ten Most Expensive Products";
con.Open();
using (IDataReader reader = com.ExecuteReader()) {
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Price of the Ten Most Expensive Products.");
while (reader.Read()) {
Console.WriteLine(" {0} = {1}", reader["TenMostExpensiveProducts"], reader["UnitPrice"]);
}
}
}
}
}
}