国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 监利县| 泽州县| 依兰县| 潮安县| 台北县| 泰州市| 阿克苏市| 都兰县| 延安市| 全州县| 阿拉善左旗| 霍邱县| 余庆县| 曲麻莱县| 黄龙县| 盱眙县| 房产| 通山县| 龙游县| 河津市| 南靖县| 庆城县| 通榆县| 六安市| 禄丰县| 沙雅县| 永宁县| 三亚市| 金溪县| 宁明县| 遵义县| 新建县| 贵德县| 梧州市| 怀宁县| 拉孜县| 长顺县| 香港| 日喀则市| 上饶县| 梨树县|