用ado.net进行分布式程序设计更新数据库(学习笔记)

来源:互联网 发布:iphone7 usb共享网络 编辑:程序博客网 时间:2024/05/16 03:04
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
提供一个数据服务类,调用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),不过程序员的控制能力要强一些了。

ado.net进行分布式程序设计更新数据库(学习笔记)';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>