国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用ado.net進行分布式程序設計更新數(shù)據(jù)庫(學習筆記)

2024-07-10 13:08:05
字體:
供稿:網(wǎng)友
提供一個數(shù)據(jù)服務類,調(diào)用getdataset就得到需要的dataset,然后在用戶界面進行修改,最后調(diào)用savedate就可以把未定的更改保存到數(shù)據(jù)庫。
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 [email protected]",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的數(shù)據(jù)庫,數(shù)據(jù)庫中建立一個education的表,表中建立了兩個字段id(int),education(char),在rc3中通過。

感覺沒有vb6.0用ado2.5中那么方便(只要設置activeconnection,然后update就一切搞定,現(xiàn)在要自己寫如何更新的sqlcommand),不過程序員的控制能力要強一些了。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜昌市| 偃师市| 来凤县| 柳河县| 宣威市| 南岸区| 太谷县| 南川市| 华阴市| 深水埗区| 信宜市| 榆中县| 东乡族自治县| 盐边县| 佛学| 东光县| 沙田区| 出国| 马龙县| 新邵县| 新营市| 木兰县| 雷山县| 徐水县| 金昌市| 福泉市| 兴安县| 合川市| 民权县| 汉寿县| 新疆| 泸定县| 吉隆县| 美姑县| 抚松县| 洪江市| 云南省| 云南省| 武胜县| 湟源县| 宜阳县|