ADO.NET之9-非串連模式,記憶體中的資料庫DataSet,DataTable---ShinePans,

來源:互聯網
上載者:User

ADO.NET之9-非串連模式,記憶體中的資料庫DataSet,DataTable---ShinePans,

DataSet被稱作資料集,可以比作記憶體中的資料庫,DataSet為ADO.NET核心,支援ADO.NET斷開式,分布式資料方案的核心對象也是實現基於非串連的資料查詢核心組件

DataTable常用屬性:

屬性 說明
Columns 擷取屬於該表的列的集合
Rows 擷取屬於該表的行的集合
TableName 擷取或設定DataTable的名稱

DataTable方法:

方法 說明
AcceptChanges 提交自上次調用AcceptChanges以來對該表進行的所有更改
Clear 清除DataTable內的所有資料
NewRow 建立與該表具有相同架構的新DataRow

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)        {            ///串連資料庫            DataSet ds = new DataSet();  //建立資料庫DataSet對象            DataTable dt = new DataTable(); //建立資料庫DataTable對象            ds.Tables.Add(dt); //將資料表對象加入資料集對象集合中            dt.Columns.Add("name", typeof(string));  //向DataTable中添加列            dt.Columns.Add("address", typeof(string));//向DataTable中添加列            DataRow dr = dt.NewRow();   //得到資料表,行對象            dr[0] = "潘潘";            dr[1] = "武漢";  //向行對象中添加資料            dt.Rows.Add(dr);            dt.Rows.Add(new object[] { "潘潘潘", "北京" }); //向資料行中添加新的對象            foreach(DataRow v in dt.Rows)            {                Console.WriteLine("{0} {1}", v[0], v[1]);            }            string connection =                "server=潘尚\\SQLEXPRESS;database=db_test;Trusted_Connection=true";            SqlConnection sc = new SqlConnection(connection);        //    sc.ConnectionString = connection;            try            {                sc.Open();  //開啟資料庫連接                Console.WriteLine("已經開啟資料庫連接!");//START:5.匯出資料庫中的記錄//////////////////////////////////////////////////////////              /*  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]);                }  *///END:5.匯出資料庫中的記錄//////////////////////////////////////////////////////////////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技術中,C#通過什的對象可以串連到SQL資料庫

using System.Data;
using System.Data.SqlClient;
..

string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";

SqlConnection objConnection=new SqlConnection(strConnection);
..

objConnection.Open();
objConnection.Close();

strConnection這個變數裡存放的是串連資料庫所需要的連接字串,他指定了要使用的資料提供者和要使用的資料來源.
首先,串連SQL Server使用的命名空間是"System.Data.SqlClient".
其次就是他的連接字串了,我們一個一個參數來介紹(注意:參數間用分號分隔):
 "user id=sa":串連資料庫的驗證使用者名稱為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
 "password=":串連資料庫的驗證密碼為空白.他的別名為"pwd",所以我們可以寫為"pwd=".
 這裡注意,你的SQL Server必須已經設定了需要使用者名稱和密碼來登入,否則不能用這樣的方式來登入.如果你的SQL Server設定為Windows登入,那麼在這裡就不需要使用"user id"和"password"這樣的方式來登入,而需要使用"Trusted_Connection=SSPI"來進行登入.
 "initial catalog=Northwind":使用的資料來源為"Northwind"這個資料庫.他的別名為"Database",本句可以寫成"Database=Northwind".
 "Server=YourSQLServer":使用名為"YourSQLServer"的伺服器.他的別名為"Data Source","Address","Addr".如果使用的是本機資料庫且定義了執行個體名,則可以寫為"Server=(local)/執行個體名";如果是遠程伺服器,則將"(local)"替換為遠程伺服器的名稱或IP地址.
 "Connect Timeout=30":連線逾時時間為30秒.

 在這裡,建立連線物件用的建構函式為:SqlConnection.
 
ADONET中DATASET與DATATABLE的具體概念

從開發的角度來說~DataSet是資料集,是針對資料的一個集合
DataTable是一張表~也就是資料
DataColumn是一張表裡面的列
DataRow 是資料的條數

用通俗的話來說
DataSet是一個檔案夾
DataTable是一個檔案
DataColumn和DataRow都是這個檔案的內容
 

相關文章

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.