用ado.net進行分布式程式設計更新資料庫

來源:互聯網
上載者:User
ado|程式|分布式|設計|資料|資料庫 提供一個資料服務類,調用GetDataSet就得到需要的DataSet,然後在使用者介面進行修改,最後調用SaveDate就可以把未定的更改儲存到資料庫。
using System;
using System.Data ;
using System.Data.SqlClient ;

namespace AsterDnet.DataBind
{
    /// <summary>
    /// Summary description for GetData.
    /// </summary>
    public class TestData
    {
        private SqlDataAdapter da;    
        public TestData()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public  DataSet GetDataSet()
        {
            
            string strConn="data source=ASTERDNET;initial catalog=Test;integrated security=SSPI;persist security info=False;user id=sa;workstation id=ASTERDNET;packet size=4096";
            SqlConnection conn=new SqlConnection(strConn);
            conn.Open();
            DataSet ds=new DataSet();

            SqlParameter workParam=new  SqlParameter();
            da=new SqlDataAdapter();
            //Build the select Command
            da.SelectCommand =new SqlCommand("select * from Education ",conn);
            da.Fill(ds,"Education");
            

            // Build the insert Command
            da.InsertCommand = new SqlCommand("Insert into Education (ID, Education) VALUES (@ID, @Education)", conn);

            workParam = da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int);
            workParam.SourceColumn = "ID";
            workParam.SourceVersion = DataRowVersion.Current;

            workParam = da.InsertCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
            workParam.SourceVersion = DataRowVersion.Current;
            workParam.SourceColumn = "Education";

            // Build the update command
            da.UpdateCommand = new SqlCommand("Update Education Set Education = @Education WHERE ID = @ID" , conn);

            workParam = da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int);
            workParam.SourceColumn = "ID";
            workParam.SourceVersion = DataRowVersion.Original;

            workParam = da.UpdateCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
            workParam.SourceVersion = DataRowVersion.Current;
            workParam.SourceColumn = "Education";

            //Build the delete command
            da.DeleteCommand =new SqlCommand("delete from Education where id=@ID",conn);
            workParam=da.DeleteCommand.Parameters.Add("@ID",SqlDbType.Int );
            workParam.SourceColumn ="ID";
            workParam.SourceVersion =DataRowVersion.Original;

            return ds;
        }
        //save the change dataset
        public void SaveDate(DataSet dataSet,string tableName)
        {
            da.Update(dataSet,tableName);
        }

    }
}

例中我建立了一個Test的資料庫,資料庫中建立一個Education的表,表中建立了兩個欄位ID(int),Education(char),在rc3中通過。

感覺沒有vb6.0用ado2.5中那麼方便(只要設定ActiveConnection,然後update就一切搞定,現在要自己寫如何更新的SqlCommand),不過程式員的控制能力要強一些了。

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。