DbProviderFactory、DbConnection、DbCommand及DbDataAdapter 使用示例
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data.Common;
5 using System.Configuration;
6 using System.Data;
7
8 namespace ECS.TestProject
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DB"];
15
16 DbProviderFactory provider = DbProviderFactories.GetFactory(settings.ProviderName);
17
18 using (DbConnection conn = provider.CreateConnection())
19 {
20 conn.ConnectionString = settings.ConnectionString;
21 conn.Open();
22 DbCommand cmd = conn.CreateCommand();
23
24 cmd.CommandText = "Select top 10 * From ShortTermBill";
25
26 DbDataAdapter da = provider.CreateDataAdapter();
27
28 da.SelectCommand = cmd;
29
30 DataSet ds = new DataSet();
31
32 da.Fill(ds);
33
34 da.Dispose();
35
36 Console.WriteLine(ds.Tables[0].Rows[0]["BillCode"]);
37 }
38 }
39 }
40 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data.Common;
5 using System.Configuration;
6 using System.Data;
7
8 namespace ECS.TestProject
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["DB"];
15
16 DbProviderFactory provider = DbProviderFactories.GetFactory(settings.ProviderName);
17
18 using (DbConnection conn = provider.CreateConnection())
19 {
20 conn.ConnectionString = settings.ConnectionString;
21 conn.Open();
22 DbCommand cmd = conn.CreateCommand();
23
24 cmd.CommandText = "Select top 10 * From ShortTermBill";
25
26 DbDataAdapter da = provider.CreateDataAdapter();
27
28 da.SelectCommand = cmd;
29
30 DataSet ds = new DataSet();
31
32 da.Fill(ds);
33
34 da.Dispose();
35
36 Console.WriteLine(ds.Tables[0].Rows[0]["BillCode"]);
37 }
38 }
39 }
40 }