利用DataAdapter於數擾庫同步

            SqlConnection conn = new SqlConnection("Data Source=192.168.27.12,10086;User ID=XX;Password=XX;Initial Catalog=XXX;Pooling=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

        
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand("select * from businesses ORDER BY TimeCreated DESC", conn);
            SqlCommandBuilder commandbuilder= new SqlCommandBuilder(da);  //用到sqlCommandBuildetr對像
            da.Fill(ds);
            conn.Close();

            Console.WriteLine(ds.Tables[0].Rows[0][0].ToString());


            DataTable dt = ds.Tables[0];

            Console.WriteLine(dt.Rows[0].RowState);

            dt.Rows[0].Delete();
        
        /*
            這裏還可以對數據進行更新,插入操作
        更新操作:dt.Rows[1][0]="XXX"
            插入一行,則先要構建列與行DataColumn,DataRow,然後相應的加入到DataTable中
            dt.Rows[1].RowState
            Console.WriteLine(dt.Rows[0].RowState);
            */

            da.Update(ds);
            ds.AcceptChanges();
posted on 2008-04-03 16:42  感動常在  阅读(384)  评论(0编辑  收藏  举报