再作一个判断,判断该主建是否在表中存在,如果存在就执行update否则就insert

解决方案 »

  1.   

    if exists(select id from mytale)
    begin
       //Update
    end
    else
    begin
      //insert
    end
      

  2.   

    用存储过程来实现。
    如 billydong(billydong) 所说:
    if exists(select id from mytale)
    begin
       //Update
    end
    else
    begin
      //insert
    end
      

  3.   

    判断命令如下:
     string str="select count(*) from table where id ="+你的id.tostring ()
     sqlconnection conn= 你的连接
     sqlcommand cm = new sqlcommand (str,conn);
     int result = 0;
     result =(int) cm.executsalary();
     if result > 0
        更新
     esle
        插入
      

  4.   

    if exists(select id from mytale where id = "你要判断的主键")
    begin
       //Update
    end
    else
    begin
      //insert
    end
      

  5.   

    用存储过程来实现。
    select @ll=COUNT(*) from mytale
    if @ll>0 then
    begin
       //Update
    end
    else
    begin
      //insert
    end
      

  6.   

    SqlConnection conn = new SqlConnection( "connetionString" );
    conn.open();
    Dataset dataset = new DataSet();
    SqlDataAdapter dscommand = new SqlDataAdapter();
    dscommand.SelectCommand = new SqlCommand( "Select id from table", conn);
    dscommand.Fill( dataset );bool exist = false;
    foreach( DataRow dr in dataset.Tables[0].Rows)
    {
       if( convert.ToInt32( dr["id"] ) == id )
        {
          exist = true;
        }
       else { exist = false; }
    }if( exist )
    {
    // update;
    }
    else
    { // insert }