错误显示:ExecuteScalar:尚未连接属性 initialized和exists =(int)cmd.ExecuteScalar()> 0;
bool exists = false;
using (SqlCommand cmd = new SqlCommand("select * from [Users] where UserName = @UserName"))
{
cmd.Parameters.AddWithValue("UserName", tbUserName.Text);
exists = (int)cmd.ExecuteScalar() > 0;
}
if (exists)
{
lblUserName.Text = "This username has been used by another user.";
}
else
{
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
cmd = new SqlCommand("INSERT INTO Users (UserID,FName, LName, PhoneNo, Address, Email, UserName, Password, Points, Role) VALUES (@UserID,@FName, @LName, @PhoneNo, @Address, @Email, @UserName, @Password, @Points, @Role)");
try
{
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@UserID", UserID);
cmd.Parameters.AddWithValue("@FName", tbFName.Text);
cmd.Parameters.AddWithValue("@LName", tbLName.Text);
cmd.Parameters.AddWithValue("@PhoneNo", tbPhoneNo.Text);
cmd.Parameters.AddWithValue("@Address", tbAddress.Text);
cmd.Parameters.AddWithValue("@Email", tbEmail.Text);
cmd.Parameters.AddWithValue("@UserName", tbUserName.Text);
cmd.Parameters.AddWithValue("@Password", tbPassword.Text);
cmd.Parameters.AddWithValue("@Points", Points);
cmd.Parameters.AddWithValue("@Role", Role);
connection.Open();
cmd.ExecuteNonQuery();
}
finally
{
connection.Close();
//session
Session["UserName"] = tbUserName.Text;
Session["UserID"] = ("SELECT * FROM Users WHERE UserID = 'UserID'");
Session["Points"] = ("SELECT * FROM Users WHERE Points = 'Points'");
//pop out then redirect
ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>alert('Thank you or signing up with us!');window.location='Home.aspx';</script>'");
}
}
首先声明连接的正确方法应该是什么,因为我之前尝试过将其放入,但是我遇到了cmd问题。
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
bool exists = false;
using (SqlCommand cmd = new SqlCommand("select * from [Users] where UserName = @UserName"))
{
cmd.Parameters.AddWithValue("UserName", tbUserName.Text);
exists = (int)cmd.ExecuteScalar() > 0;
}
答案 0 :(得分:1)
您需要在上一个示例中为该命令分配连接
所以在你的using语句中添加:
cmd.Connection = connection;
此外,您不需要:
SqlCommand cmd;
因为命令是在using
语句的上下文中创建的。
将Connection
上下文包含在using语句中以确保正确处理/关闭实际连接也是一种良好的做法。
答案 1 :(得分:1)
试试这个,
using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
{
connection .Open();
SqlCommand cmd = new SqlCommand("select * from [Users] where UserName = @UserName", connection );
cmd.Parameters.AddWithValue("UserName", tbUserName.Text);
bool exists = (int)cmd.ExecuteScalar() > 0;
}
您未将connection
指定为command
。其次,你必须打开连接。
更新:完整代码
try
{
string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
bool exists = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection .Open();
SqlCommand cmd = new SqlCommand("select * from [Users] where UserName = @UserName", connection );
cmd.Parameters.AddWithValue("UserName", tbUserName.Text);
exists = (int)cmd.ExecuteScalar() > 0;
}
if (exists)
{
lblUserName.Text = "This username has been used by another user.";
}
else
{
using(SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Users (UserID,FName, LName, PhoneNo, Address, Email, UserName, Password, Points, Role) VALUES (@UserID,@FName, @LName, @PhoneNo, @Address, @Email, @UserName, @Password, @Points, @Role)", Connection);
cmd.Parameters.AddWithValue("@UserID", UserID);
cmd.Parameters.AddWithValue("@FName", tbFName.Text);
cmd.Parameters.AddWithValue("@LName", tbLName.Text);
cmd.Parameters.AddWithValue("@PhoneNo", tbPhoneNo.Text);
cmd.Parameters.AddWithValue("@Address", tbAddress.Text);
cmd.Parameters.AddWithValue("@Email", tbEmail.Text);
cmd.Parameters.AddWithValue("@UserName", tbUserName.Text);
cmd.Parameters.AddWithValue("@Password", tbPassword.Text);
cmd.Parameters.AddWithValue("@Points", Points);
cmd.Parameters.AddWithValue("@Role", Role);
cmd.ExecuteNonQuery();
}
}
}
catch(Exception ex)
{
//Do something
}
finally
{
//session
Session["UserName"] = tbUserName.Text;
Session["UserID"] = ("SELECT * FROM Users WHERE UserID = 'UserID'");
Session["Points"] = ("SELECT * FROM Users WHERE Points = 'Points'");
//pop out then redirect
ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script type='text/javascript'>alert('Thank you or signing up with us!');window.location='Home.aspx';</script>'");
}
答案 2 :(得分:0)
SQL命令应该有一个过载,它需要一个启动的连接
答案 3 :(得分:0)
使用SqlCommand
构造函数将连接作为第二个参数。接下来,你必须在执行命令之前打开它,然后关闭它。
using (SqlCommand cmd = new SqlCommand("select * from [Users] where UserName = @UserName", connection))
{
cmd.Parameters.AddWithValue("UserName", tbUserName.Text);
connection.Open();
exists = (int)cmd.ExecuteScalar() > 0;
connection.Close();
}