sql server查询数据是否存在

来源:百度知道 编辑:UC知道 时间:2024/06/20 19:06:40
做登录,如果查询数据库有个户名已存在给出提示
现在写了查询语句:string str = "select * from user where username='" + username + "'";
请问接下来应该用什么方法查询数据库,返回用户名是否存在??
麻烦大家帮我写几句

string str = "select count(*) from user where username='" + username + "'";

这样就成了。

你的代码不完全,要转换!

DataTable dt ="select * from user where username='" + username + "'";
if(dt.Rows.count < 0)
{
Message.Show("用户名可以用");
}
else
{
Message.Show("用户名已存在");
}

string str = "select * from user where username='" + username + "'";

int count = ExecuteNonQuery(str);

public int ExecuteNonQuery(string cmdText)
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
cmd.CommandText = cmdText;
cmd.Connection = connection;
if (connection.State != System.Data