DECLARE key1_cursor scroll cursor For
select LoginName from Msg_UserInfo where LoginName='LoginName'
open key1_cursor
If (@@cursor_rows=0)
Begin
insert into Msg_UserInfo (LoginName) values('LoginName')
End
Close key1_cursor   

解决方案 »

  1.   

    不知楼上的   DECLARE key1_cursor scroll cursor For   什么意思啊
    我一般是直接查询的
      

  2.   

    添加前先select一下,看是否存在,然后再作相应的操作
      

  3.   

    可不可以这么写?
    string sql="SELECT * FROM tablename WHERE 字段名称 ='"+txtLogin.Text.ToString().Trim()+"'";
    SqlDataAdapter adapter=new SqlDataAdapter (sql ,this.SQLConnection1 );
    DataSet dr=new DataSet ();
    this.Connect ();
    adapter.Fill (dr,table);
    this.Disconnect ();
    if(dr.Tables[this.UserTableName ].Rows.Count!=0)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~判断是否已经有这个值了
      

  4.   

    以下被接受为正确答案!/************************************************************************************/
        /* 根据字符串参数name判断是否重名
        /* string name:要查找的符合条件的字符串
        /************************************************************************************/
        //
    public bool SameName(string colName, string name)
    {
    object obj=new object(); SqlCommand sqlCommand = new SqlCommand( "SELECT * FROM person WHERE "+ colName + " = '"+ name.Trim() +"'",conn);

    try
    {
    //打开数据库的连接
    conn.Open(); obj = sqlCommand.ExecuteScalar();
    }
    catch(SqlException e)
    {
    MessageBox.Show(e.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    }
    finally
    {
    //关闭数据库的连接
    conn.Close(); } if (obj != null)
    {
    //这个职称的信息已经填加了!
    return false;
    }
    else
    {
    //这个职称没有填加,可以正常操作了。
    return true;
    }
    }
      

  5.   

    "if not exists (select * from Msg_UserInfo where LoginName = "+txtLogin.Text.ToString().Trim()+") insert into Msg_UserInfo (LoginName) values('"+txtLogin.Text.ToString().Trim()+"')"
      

  6.   

    把LoginName中的内容放到dataset: ds 中。
         
         ArrayList NameList=new ArrayList ();
         NameList.Clear();
         for(int i = 0;i<ds.Tables[0].Rows.Count;i++)
          {
    object[] objArray = ds.Tables[0].Rows[i].ItemArray;
    NameList.Add(objArray[0].ToString());
    }   用的时候判断NameList中是否有你要增加的用户即可。      for(int i=0;i<NameList.Count;i++)
    {
            bool GetItem=NameList.Contains(LoginName);
    if(GetItem==false)
    {
    //插入记录;
                  break;
    }
               else
              {
                   //消息
               }
     }