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
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
我一般是直接查询的
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~判断是否已经有这个值了
/* 根据字符串参数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;
}
}
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
{
//消息
}
}