标签:
相关知识:
主要代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data; 7 using System.Data.Common; 8 9 namespace ConsoleApplication16 10 { 11 class Program 12 { 13 const string provider = "System.Data.SqlClient"; 14 const string strConn = @"server=Joe-PC;database=AccountDBforSQLInjection;uid=sa;pwd=root"; 15 16 static void Main(string[] args) 17 { 18 //获取数据提供程序工厂类 19 DbProviderFactory factory = DbProviderFactories.GetFactory(provider); 20 21 //通过工厂创建连接对象 22 DbConnection conn = factory.CreateConnection(); 23 conn.ConnectionString = strConn; 24 25 //通过工厂创建命令对象 26 DbCommand cmd = factory.CreateCommand(); 27 cmd.Connection = conn; 28 cmd.CommandText = "SELECT AccountID, AccountName, password FROM Account"; 29 30 try 31 { 32 conn.Open(); 33 DbDataReader dr = cmd.ExecuteReader(); 34 35 while (dr.Read()) 36 { 37 Console.WriteLine("{0}:{1},{2}", dr[0], dr[1], dr[2]); 38 } 39 } 40 catch (Exception e) 41 { 42 Console.WriteLine(e); 43 } 44 finally 45 { 46 conn.Close(); 47 } 48 } 49 } 50 }
标签:
原文地址:http://www.cnblogs.com/chenguangqiao/p/4373539.html