数据库连接文件:
public static SqlConnection getConnection()
{
return new SqlConnection("server=.;database=QQ;user=sa;password=sa");
} public bool selectuser(string Name)
{
String sql = string.Format("select * from userinfo where name='{0}'", Name);
try
{
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
int i = com.ExecuteNonQuery();
if (i == -1)
{
return true;
}
else
{
return false;
} }
catch (Exception)
{ throw new Exception("数据库连接出错或者产生了其它错误!");
}
finally
{
conn.Close();
}
}
主窗体上的添加按钮代码如下:
private void button1_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("对不起,姓名不能为空!");
return;
}
Connection conn = new Connection();
if (conn.selectuser(this.txtName.Text.Trim()))
{
MessageBox.Show("数据库中已经存在该用户,\n用户名不能重复,请重新添加!");
return;
}
else
{
conn.insertinfo(this.txtName.Text, this.txtPwd.Text, sex, txtEmail.Text, int.Parse(txtAge.Text), txtAddress.Text, grounp, this.textDescn.Text);
getData();
}
}
现在出现了这样一个问题:
不管数据库中是否存在该用户,都执行:
MessageBox.Show("数据库中已经存在该用户,\n用户名不能重复,请重新添加!");
并且不执行添加语句,是在是不知道错哪里了
希望知道的朋友指教下,谢谢了!
public static SqlConnection getConnection()
{
return new SqlConnection("server=.;database=QQ;user=sa;password=sa");
} public bool selectuser(string Name)
{
String sql = string.Format("select * from userinfo where name='{0}'", Name);
try
{
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
int i = com.ExecuteNonQuery();
if (i == -1)
{
return true;
}
else
{
return false;
} }
catch (Exception)
{ throw new Exception("数据库连接出错或者产生了其它错误!");
}
finally
{
conn.Close();
}
}
主窗体上的添加按钮代码如下:
private void button1_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
MessageBox.Show("对不起,姓名不能为空!");
return;
}
Connection conn = new Connection();
if (conn.selectuser(this.txtName.Text.Trim()))
{
MessageBox.Show("数据库中已经存在该用户,\n用户名不能重复,请重新添加!");
return;
}
else
{
conn.insertinfo(this.txtName.Text, this.txtPwd.Text, sex, txtEmail.Text, int.Parse(txtAge.Text), txtAddress.Text, grounp, this.textDescn.Text);
getData();
}
}
现在出现了这样一个问题:
不管数据库中是否存在该用户,都执行:
MessageBox.Show("数据库中已经存在该用户,\n用户名不能重复,请重新添加!");
并且不执行添加语句,是在是不知道错哪里了
希望知道的朋友指教下,谢谢了!
getData()是一个吧数据显示在datagridview的方法
try
{
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery();
if((int)com.ExecuteScalar()>0)
{
//存在
return true;
}
else
{
//不存在
return false;
} }
if (i == -1)
{
return true;
}
else
{
return false;
}这个似乎有问题,因为不可能会为-1,数据表为空时,也不会是1,最少也只是0。
string sqlconn = "data source=.;database=em;uid=sa;pwd=";
SqlConnection ss = new SqlConnection(sqlconn);
ss.Open();
SqlDataAdapter sd = new SqlDataAdapter(sql, ss);
DataSet yy = new DataSet();
sd.Fill(yy);
ss.Close();
int dd = yy.Tables[0].Rows.Count;
if (dd > 0)
{
MessageBox.Show("已存在相同编号的数据,请修改", "提示");
textBox1.Clear();
textBox1.Focus();
}
当i == -1时说明没有重名用户你怎么还返回true 啊。
{
return false;
}
else
{
return true;
}
if (i > 0)
{
return true;
}
else
{
return false;
}
{
bool flag = false;
string sellogin = "select User_Name,User_Pwd from User_Login where User_Name='"+username+"';
conn.Open();
cmd = new OleDbCommand(sellogin, conn);
dr=cmd.ExecuteReader();
if (dr.Read())
{
flag = true;
}
else
{
flag = false;
}
conn.Close();
return flag;
}click事件里写
flag = db.SelUser_Login(name);
if (flag == true)
{
MessageBox.Show("用户有重复");
}
LZ试下