本文整理匯總了C#中System.Data.Common.DbProviderFactory.CreateConnection方法的典型用法代碼示例。如果您正苦於以下問題:C# DbProviderFactory.CreateConnection方法的具體用法?C# DbProviderFactory.CreateConnection怎麽用?C# DbProviderFactory.CreateConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了DbProviderFactory.CreateConnection方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Data;
using System.Data.Common;
class MainClass
{
public static void Main(string[] args)
{
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
using (IDbConnection con = factory.CreateConnection())
{
con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
using (IDbCommand com = con.CreateCommand())
{
com.CommandType = CommandType.Text;
com.CommandText = "SELECT ID, FirstName FROM Employee";
con.Open();
using (IDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
// Display the product details.
Console.WriteLine(" {0} = {1}",
reader["ID"],
reader["FirstName"]);
}
}
}
}
}
}