点点滴滴 访问量:
posts - 90,comments - 141,trackbacks - 8
http://www.cnblogs.com/ZeroCool/archive/2006/11/28/574645.html
  今天看了几篇Autumoon兄的文章,收获很多,想法也很多,但是不知从何下笔,就随便写点东西。
  来北京快有三年了,前两年还处于边学习边打工阶段,其实打工也只是维持自己的生活而已,觉得自己已经长大了,不想再向家里伸手了,
虽然这两年过得很辛苦,但是收获蛮多的,1。好的经理 专注做一件事情。2,没有解决不了的事,只要你坚持
posted @ 2007-01-05 18:59 sopper 阅读(98) 评论(0) 编辑
  方法一:在对SQL数据库进行更新时,用CommandBuilder对像来自动构建sql命令,来起到更新的作用;这种方法用起来比较方便,具体代码如下:

以下代码都在xp系统下测试通过

环境:vs.net2005 \ sql server 2000\xp
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace sqltest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{

            
string sConnectonString;
            sConnectonString 
= "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
            SqlConnection objConn 
= new SqlConnection(sConnectonString);
            objConn.Open();
                       
            SqlDataAdapter daAuthors 
= new SqlDataAdapter("select * from city_users", objConn);
            DataSet dsMaster 
= new DataSet("master");
            daAuthors.FillSchema(dsMaster, SchemaType.Source, 
"city_users");//加载表的构架,这样在操作加载到dataset里的表时就不用重新设置主键了
            daAuthors.Fill(dsMaster, "city_users");
            dsMaster.Tables[
"city_users"].Rows[1]["user_name"= "sql";//注意这里的Rows[1],这个1指得是在DataSet里的表里的行号,其与数据库里的行号不一致如图1
            
            SqlCommandBuilder objCommandBuilder 
= new SqlCommandBuilder(daAuthors);//构建sql命令
            daAuthors.Update(dsMaster, "city_users");//更新
            
//daAuthors.UpdateCommand = updatecomm;
            
//daAuthors.UpdateCommand.ExecuteNonQuery();
            objConn.Close();
            Console.WriteLine(
"update was successful");
         
        }

    }

}



图1


方法二:手动添加sql语句,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace sqltest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{

            
string sConnectonString;
            sConnectonString 
= "Password=sa;user id = sa;Initial Catalog = master;Data Source=(local)";
            SqlConnection objConn 
= new SqlConnection(sConnectonString);
            objConn.Open();
            SqlCommand updatecomm 
= new SqlCommand("update city_users set user_name='mysql' where user_id = 7", objConn); 
            daAuthors.UpdateCommand 
= updatecomm;
            daAuthors.UpdateCommand.ExecuteNonQuery();
            objConn.Close();
            Console.WriteLine(
"update was successful"); 
        }

    }

}



为什么我要写这一篇呢?原因是我在用MySql时,用的MySql驱动没有CommandBuilder对像,不能自动构建sql命令,所以就有了这篇文章。
posted @ 2007-01-05 11:54 sopper 阅读(186) 评论(0) 编辑