我的数据表: 
字段              数据类型     备注
ID int         (key)
clientIP          char
clientBrowser char
clientSystem char
clientTime datetime我老是写错SQl语句,我想写个函数 
public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime){}
有劳各位大虾帮忙写个函数体啊!!!实现对表中数据的修改!
    

解决方案 »

  1.   

    SQL="update 表 set ID='"+ID+"',clientIP='"+clientIP+"',clientBrowser='"+clientBrowser+"',clientBrowser='"+clientSystem+'",clientSystem='"+clientSystem+"',clientTime='"+clientTime+"' where ID='"+ID+"'";
      

  2.   

    不好意思看错了ID 是intSQL="update 表 set ID="+ID+",clientIP='"+clientIP+"',clientBrowser='"+clientBrowser+"',clientBrowser='"+clientSystem+'",clientSystem='"+clientSystem+"',clientTime='"+clientTime+"' where ID="+ID;
      

  3.   

    public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime){
    string sql ="update Table Set Ip = "+clientIP+",Browers="+clientBrowser+",clientSystem="+clientSystem+",clientTime="+clientTime+" where ID="+ID;SqlConnection conn = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = sql;
    conn.open();
    cmd.ExecuteQuery();}
      

  4.   

    有没有insert的例子阿,麻烦贴出来
      

  5.   

    public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime){
    string sql ="update Table Set Ip = "+clientIP+",Browers="+clientBrowser+",clientSystem="+clientSystem+",clientTime="+clientTime+" where ID="+ID;SqlConnection conn = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = sql;
    conn.open();
    cmd.ExecuteQuery();
    conn.close();//請關閉。:)
    }
      

  6.   

    你用组件类生成不就行了吗?新建一个组件类,拖一张表进去,select insert update delete
    语句都有了,调用Adapter.UpDate(ds) 方法一切搞定!,如果你愿意自己写也没问题,把那个里面自动生成的Command语句Copy出来:)不过有一点要注意,组建类自动生成时SqlConnection的ConnectionString是写死的,建议动态改
      

  7.   

    public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime)
    {
    SqlConnction conn=new SqlConnection("server=localhost;uid=sa;pwd=;database=yourdb");
    SqlCommand cmd=new SqlCommand("update tablename set clientIP=@clientIP,clientBrowser=@clientBrowser,.... where ID=@ID",cnn);
    cmd.Parameters.Add("@clientIP",clientIP);
    .....
    cmd.Parameters.Add("@ID",ID);
    try
    {
    cnn.open();
    cmd.ExecuteNonquery();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }}
      

  8.   

    十分谢谢大家的帮助啊!跟新是我SQL语句错了 用查询分析器检查过后 就搞定了。
    不过我现在还是举步艰难,困惑如下:
    //*********************
    现在我目前的困境是这样的:
    1)页面上有3个TextBox 和一个Button(修改数据库的按钮)
       TextBox tbCid;TextBox tbClient;TextBox tbDatetime;Button Button1;
    2)每次加载页面的时候读数据库并把一条记录的相关信息写入3个TextBox ;
     然后我修改:TextBox tbClient;TextBox tbDatetime 中的字符、点吉Button Button1
     向数据库中跟新数据。
     发现数据库中数据没有修改,由这一句输出:Response.Write(sql + "<hr>");
     发现其中的:TextBox tbClient;TextBox tbDatetime 中的数据还是页面加载时的值,并没有
     随着我的跟改而改变!
    具体代码如下:
    //***********************
    private void Page_Load(object sender, System.EventArgs e)
            {
    InitData();
            }    private void Button1_Click(object sender, System.EventArgs e)
            {
                                       string sql = "update Client set clientIP='" + tbClient.Text + 
                            "'clientTime='" + tbDatetime.Text + "' "+"where ID="+tbCid.Text;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = GetConnection();
                cmd.CommandText = sql;
                Response.Write(sql + "<hr>");
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message + "\t" + ex.StackTrace);
                } 
                Response.Write("success");
                }
            }        public void InitData()
            {
                string sql = " select ID,clientIP,clientTime from Client ";
                DataTable dt = new DataTable("Client");
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(sql,GetConnection());
                    da.Fill(dt);
                }
                catch(Exception e)
                {
                    dt = null;
                    Response.Write(e.Message + "\t" + e.StackTrace);            }
                tbCid.Text = dt.Rows[0]["ID"].ToString();
                tbClient.Text = dt.Rows[0]["clientIP"].ToString();
                tbDatetime.Text = dt.Rows[0]["clientTime"].ToString();
            }        public SqlConnection GetConnection()
            {
                
                SqlConnection sqlConnection;
                sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = "Server=(local);user=sa;password=1;database=pubs";;
                try
                {
                    sqlConnection.Open();
                }
                catch(Exception e)
                {
                    Response.Write(e.Message + "\t" + e.StackTrace);
                    return null;
                }
                return sqlConnection;
            }
        }
      

  9.   


    if(!IsPostBack)
    {
       InitData();
    }
      

  10.   

    private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!IsPostBack)
                      {
    InitData();
                       }
            }原因很简单,因为你点更新按钮时,postback也同时要pageload,用
    (!IsPostBack)过滤点postback回来的动作.
      

  11.   

    private void Page_Load(object sender, System.EventArgs e)
            {
    if(!IsPostBack)
    {
    InitData();
     }
           }
    建议你多看看书,这些都是最基本的。
      

  12.   

    看了你发的短信息过来的。建议楼主多看看基础教程
    private void Page_Load(object sender, System.EventArgs e)
            {
    if(!IsPostBack)
    {
    InitData();
     }
           }
      

  13.   

    心中感激各位高手的帮助啊..
    大侠们的指导小弟一定铭记心中.
    一直在看CET-6刚过,现在好好学习编程.
    低起点..