ADO.NET之8-資料讀取器,DataReader---ShinePans,

來源:互聯網
上載者:User

ADO.NET之8-資料讀取器,DataReader---ShinePans,

根據資料提供者不同,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 student_name=@name", 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 student_name=@name";                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();        }    }}

運行結果:





ADONET 緊急進

System; --包含.net最基礎的類
System.ComponentModel; --提供用於實現組件和控制項運行時和設計時行為的類
System.Data; --資料訪問
System.Web; --提供使得可以進行瀏覽器與伺服器通訊的類和介面System.Web.SessionState; --提供可將特定於某個單個用戶端的資料存放區在伺服器上的一個 Web 應用程式中的類和介面
System.Web.UI;WEB使用者介面類
System.Web.UI.WebControls;--WEB控制項集合
System.Web.UI.HtmlControls;--HTML控制項集合
參考資料:MSDN
 
C# 背景程式,用ADOnet 怎讀取EXCEL中資料到DataSet

publicDataSet ImportExcel(string strFileName) //參數為檔案路徑+檔案名稱。就是DataSource
{
if (strFileName != "")
{
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties=Excel 8.0";
string sql = "select * from [Sheet1$]";
OleDbDataAdapter da = newOleDbDataAdapter(sql, conn);
DataSet ds = newDataSet();
try
{
da.Fill(ds, "datatable");
}
catch
{

}
return ds;
}
else
{
return null;
}
}
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.