標籤:blog http 使用 os io 資料 2014 art
根據資料提供者不同,DataReader可分為SqlDataReader,OleDbDataReader,OlbeDataReader和OracleDataReader等4大類
一個巧妙的比喻:如果資料庫是水庫,那麼SqlConnection是進水笼頭,SqlCommand是抽水機,SqlDataReader是出水的水管,SqlDataReader每次只能讀取一條記錄,每當SqlDataReader調用Read方法就會從資料庫得到一條記錄,同時Read方法會返回False值,可以使用Wihle迴圈來調用SqlDataReader的Read方法,讀取資料庫中的記錄,SqlDataReader的工作方式意味著,在讀取資料庫的時候要保持與資料庫的串連,如果此時中斷連線,資料會讀取失敗.
對於SqlCommand對象調用ExecuteScalar方法來查詢表中記錄的數量,SqlCommand對象調用ExecuteDataReader方法,查詢表中所有的記錄
原始碼:
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SQLTest{ class Program { static void Main(string[] args) { ///串連資料庫 string connection = "server=潘尚\\SQLEXPRESS;database=db_test;Trusted_Connection=true"; SqlConnection sc = new SqlConnection(connection); // sc.ConnectionString = connection; try { sc.Open(); //開啟資料庫連接 Console.WriteLine("已經開啟資料庫連接!"); SqlCommand cmd = new SqlCommand("SELECT * FROM db_student", sc); SqlDataReader sdr = cmd.ExecuteReader(); //執行尋找記錄命令 while(sdr.Read()) { Console.WriteLine("{0}{1}{2}{3}", sdr[0], sdr[1], sdr[2], sdr[3]); }//START:4.查詢資料庫記錄////////////////////////////////////////////////////////////// /* SqlCommand cmd = new SqlCommand("SELECT count(*) FROM db_student", sc); int i = (int)cmd.ExecuteScalar();//執行尋找記錄的命令 Console.WriteLine("表中共有{0}條資料", i.ToString()); *///END:4.查詢資料庫記錄//////////////////////////////////////////////////////////////////START:3.修改資料庫資料的代碼//////////////////////////////////////////////////////// /* SqlCommand cmd = new SqlCommand("UPDATE db_student SET student_grade=99 where [email protected]", sc); //建立SqlCommand對象 cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "潘"; int i = cmd.ExecuteNonQuery(); if (i > 0) Console.WriteLine("修改成功!"); *///END:3.修改資料庫資料的代碼///////////////////////////////////////////////////////////START:1.刪除資料庫記錄程式碼片段/////////////////////////////////////////////////////// /* string cmdtext = "DELETE FROM db_student WHERE [email protected]"; SqlCommand cmd = new SqlCommand(cmdtext, sc); cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "潘"; int i = cmd.ExecuteNonQuery(); if (i > 0) Console.WriteLine("刪除記錄成功!"); *///END:1.刪除資料庫記錄程式碼片段///////////////////////////////////////////////////////////START:2.添加記錄的代碼/////////////////////////////////////////////////////////////// /* SqlCommand cmd = new SqlCommand();//建立SqlCommand對象 cmd.CommandType = CommandType.Text; //設定執行文本命令 cmd.Connection = sc; //設定對象屬性 cmd.CommandText = "INSERT INTO db_student(student_name,student_age,student_address,student_grade)VALUES(@name,@age,@address,@grade)"; //添加參數並為參數賦值 cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = "潘"; cmd.Parameters.Add("@age", SqlDbType.Int).Value = 19; cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = "武漢"; cmd.Parameters.Add("@grade", SqlDbType.Int).Value = 100; int i = cmd.ExecuteNonQuery(); //執行資料庫添加記錄命令 if (i > 0) Console.WriteLine("添加記錄成功"); */ //控制台輸出添加記錄 //END:2.添加記錄的代碼///////////////////////////////////////////////////////////////// } catch (Exception ex) { Console.WriteLine("開啟資料庫錯誤:{0}", ex.Message); } finally { sc.Close(); Console.WriteLine("資料庫連接已關閉!"); } System.Console.ReadLine(); } }}
運行結果: