选定一个列, 比如用户编号列
- // 欲插入的用户编号
- string ll_userID="xxxxxxxx";
- // 查询此编号是否存在
- SqlCommand mycmd = new SqlCommand("select 用户编号 from table where 用户编号 ='"+ll_userID+"'", mycon);
- SqlDataReader mysdr = mycmd.ExecuteReader();
- if (mysdr.HasRows)
- {
- // 已经有记录使用此编号
- }
- else
- {
- // 此编号未被使用
- }
- mysdr.Close();
- mycon.Close();
---------- 如果用户编号列是整型的话, 也可以写成这样子
- int ll_userID;
- // 取出当前记录中最大编号值
- SqlCommand mycmd = new SqlCommand("select max(用户编号) from table", mycon);
- SqlDataReader mysdr = mycmd.ExecuteReader();
- // 这里还检测是否有记录的目的是为了判断表是否为空
- if (mysdr.HasRows)
- {
- // 表不为空, 在查询得到的最大编号基础上 + 1, 然后插入新纪录
- }
- else
- {
- // 表为空, 则插入一个默认的最小编号记录
- }
- mysdr.Close();
- mycon.Close();
来源: https://www.cnblogs.com/sdxlz666/p/11029605.html