ADO.NET之5-使用Command對象刪除記錄---ShinePans,

來源:互聯網
上載者:User

ADO.NET之5-使用Command對象刪除記錄---ShinePans,

原始碼:


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("已經開啟資料庫連接!");                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("刪除記錄成功!");                //////添加記錄的代碼//////            /*    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("添加記錄成功"); //控制台輸出添加記錄             * ////添加記錄的代碼///////*/            }            catch (Exception ex)            {                Console.WriteLine("開啟資料庫錯誤:{0}", ex.Message);            }            finally            {                sc.Close();                Console.WriteLine("資料庫連接已關閉!");            }            System.Console.ReadLine();        }    }}

刪除執行:




刪除後的狀態:





總結:注意cmdtext的正確性


敘述ADONET中主要的5個類及其功可以

Connection 物件
Connection 對象主要是開啟程式和資料庫之間的連結。沒有利用連結對象將資料庫開啟,是無法從資料庫中取得資料的。這個物件在ADO.NET 的最底層,我們可以自己產生這個對象,或是由其它的對象自動產生。

Command 物件
Command 對象主要可以用來對資料庫發出一些指令,例如可以對資料庫下達查詢、新增、修改、刪除資料等指令,以及呼叫存在資料庫中的預存程式等。這個對象是架構在Connection 對象上,也就是Command 對象是透過連結到資料來源

DataAdapter 物件
DataSetCommand 對象主要是在資料來源以及DataSet 之間執行資料轉送的工作,它可以透過Command 對象下達命令後,並將取得的資料放入DataSet 對象中。這個對象是架構在Command對象上,並提供了許多配合DataSet 使用的功能。在Beta 2 版中DataSetCommand 物件會更名為DataAdapter。

DataSet 物件
DataSet 這個對象可以視為一個暫存區(Cache),可以把從資料庫中所查詢到的資料保留起來,甚至可以將整個資料庫顯示出來。DataSet 的能力不只是可以儲存多個Table 而已,還可以透過DataSetCommand 對象取得一些例如主鍵等的資料表結構,並可以記錄資料表間的關聯。DataSet 對象可以說是ADO.NET 中重量級的對象,這個對象架構在DataSetCommand 對象上,本身不具備和資料來源溝通的能力;也就是說我們是將DataSetCommand 對象當做DataSet 對象以及資料來源間傳輸資料的橋樑。

DataReader 物件
當我們只需要循序的讀取資料而不需要其它操作時,可以使用DataReader 對象。DataReader對象只是一次一筆向下循序的讀取資料來源中的資料,而且這些資料是唯讀,並不允許作其它的操作。因為DataReader 在讀取資料的時候限制了每次唯讀取一筆,而且只能唯讀,所以使用起來不但節省資源而且效率很好。使用DataReader 對象除了效率較好之外,因為不用把資料全部傳回,故可以降低網路的負載。
參考資料:hi.baidu.com/ksfangdao
 
列舉ADONET中的五個主要對象,並簡單描述

connection 連線物件
command 命令對象,指示要執行的命令和預存程序!
datareader是一個向前的唯讀資料流。
dataadapter是功能強大的適陪器,支援增刪改查的功能
dataset是一個資料級對象,相當與記憶體中的一張表或多張表!
 

相關文章

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.