看了篇文章介绍c#对数据的操作介绍,具体方法如下(转载)
1.添加记录 (添加行)
  添加记录首先需要声明两个变量 DataTable ,DataRow
  其中DataTable需要实例化到具体的数据集中的某个table
  DataRow = DataTable.NewRow()  声明为Table的新Row
  再对DataRow进行赋值,调用DataTable.Rows.Add(DataRow)即可
 2.修改纪录 (编辑行)
  首先声明一个变量 DataRow[] objRows用来存储要编辑的行
  objRows = DataTable.Select("查询条件"); 
  如果是一行,可以这样 objRows = DataTable.Rows[3];
  再对其进行修改 如 objRows[0][FIELD1]=""  objRows[0][FIELD2]=""
 3.删除纪录 
  如下 DataTable.Rows[5].Delete(); 
  推想:应该可以这样,首先申明一个变量 DataRow[] objRows 用来存储要删除的行
  objRows = DataTable.Select("查询条件");提出问题:
1在增加、修改、删除记录时,是否可以用sql语句呢?如:insert into (update,delete)等...
2如果可以的话,想请教各位,那种方式更好些呢?或者是那种方式更适合用在某个条件下呢?

解决方案 »

  1.   

    ---- 可以,我就喜欢这么弄,要先生命数据库连接,数据库访问对象,如oledbCommand,然后,自己写sql,执行(ExcuteNoQuery方法),很方便的。
    我觉得这种方法比较好,适用于分层的程序设计,自己再封装个数据库访问类,用起来更方便的。
      

  2.   

    Of course you can. Two ways will lead you there!Way1. use SqlCommand.ExcuteNoQuery() Mathod, which will return the count of affected records
    Way2. use DataSet with SqlDataAdapter and SqlCommandBuilder, which I perfer:) 
          It can give you a magic way to handle data-accessing by automatically generating SQL query string with appropiate parameters, only thing you need to do to save your change to a DataSet object is to invoke mathod SqlDataAdapter.Update() after calling the SqlCommandBuilder.RefreshScheme().you can refer to MSDN[http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlcommandbuilderclasstopic.asp] for more information.
      

  3.   

    修改:
    private void Button4_Click(object sender, System.EventArgs e)
    {
    SqlConnection myconnection = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=demodatabase");
    myconnection.Open();
    SqlCommand sqlCommand2 = new SqlCommand("",myconnection);
    sqlCommand2.CommandText = "update logintable set name='修改001' where name='001'";
    sqlCommand2.ExecuteNonQuery(); }
    //删除
    private void Button5_Click(object sender, System.EventArgs e)
    {
    SqlConnection myconnection = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=demodatabase");
    myconnection.Open();
    SqlCommand sqlCommand2 = new SqlCommand("",myconnection);
    sqlCommand2.CommandText = "delete from logintable where name='test1'";
    sqlCommand2.ExecuteNonQuery();
    }
    以上两个操作重复的地方是否可以省略掉阿?( SqlConnection myconnection = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=demodatabase");
    myconnection.Open();
    SqlCommand sqlCommand2 = new SqlCommand("",myconnection);)
    每次都要new一下,这样是不是很耗内存阿!new的工作原理是怎么样的,会不会自动释放啊?
      

  4.   

    No need to New for every time you want to execute a sql command
    just do like this:
    //声明成员变量
    private SqlCommand mycmd;
    ..
    //在构造函数中初始化
    mycmd = new SqlCommand();...
    ..
    //然后使用它
    private void somemathod()
    {
        mycmd。CommandText = "....";
        mycmd.ExecuteNonQuery();    mycmd。CommandText = "....";
        mycmd.ExecuteNonQuery();
    }new引发的内存分配,如果是局部变量 在运行出作用域后会被垃圾回收机制自动释放 如果是成员变量,在调用对象的 Dispose()后会被清除/