小角落

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
初学C#,记录自己的学习笔记,

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication3
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   SqlConnection thisConnection=new SqlConnection("workstation id=ALEX;packet size=4096;user id=sa;data source=ALEX;persist security info=False;initial catalog=pubs");
   thisConnection.Open();
   //更新数据
   SqlDataAdapter thisAdapter=new SqlDataAdapter("select au_ID,au_lname,au_fname,contract,phone from TB_authors",thisConnection);
            SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter);
   DataSet thisDataSet=new DataSet();
   thisAdapter.Fill(thisDataSet,"TB_authors");

         Console.WriteLine("name before change  {0}",thisDataSet.Tables["TB_authors"].Rows[9]["au_lname"]);

            thisDataSet.Tables["TB_authors"].Rows[9]["au_lname"]="acme,lcn";
   thisAdapter.Update(thisDataSet,"TB_authors");

   //插入新行

            Console.WriteLine("name after change  {0}",thisDataSet.Tables["TB_authors"].Rows[9]["au_lname"]);
   Console.WriteLine("Rows before change  {0} ",thisDataSet.Tables["TB_authors"].Rows.Count);
            DataRow thisRow= thisDataSet.Tables["TB_authors"].NewRow();
   thisRow["au_id"]="12345";
   thisRow["au_lname"]="wangwei";
   thisRow["au_fname"]="alex";
   thisRow["contract"]=1;
   thisRow["phone"]="13510780492";
       thisDataSet.Tables["TB_authors"].Rows.Add(thisRow);
   thisAdapter.Update(thisDataSet,"TB_authors");
            Console.WriteLine("Rows after change  {0} ",thisDataSet.Tables["TB_authors"].Rows.Count);
            //删除行  
    DataColumn[] keys=new DataColumn[1];
    keys[0]=thisDataSet.Tables["tb_authors"].Columns["au_ID"];
             thisDataSet.Tables["tb_authors"].PrimaryKey=keys;

             DataRow findRow=thisDataSet.Tables["tb_authors"].Rows.Find("12345");
   if (findRow!=null)
   {
               findRow.Delete();

   }

   thisAdapter.Update(thisDataSet,"TB_authors");
            Console.WriteLine("Rows Delete after change  {0} ",thisDataSet.Tables["TB_authors"].Rows.Count);
   thisConnection.Close();

  }
 }
}

posted on 2005-08-11 16:34  Alex  阅读(468)  评论(0)    收藏  举报