因此,我正在建立一个网站并实施注册功能,我登录工作但我在注册时遇到了麻烦。
基本上它没有写任何id db,我尝试了多项但没有任何作用,我没有得到任何错误或例外它只是没有在db中写任何东西,激活邮件部分工作正常,我收到邮件,但没有任何内容写在UsersInfo数据库和UsersActivations数据库。
public partial class RegistrationPage : System.Web.UI.Page
{
protected void ActivationMail(int userId)
{
string constr = ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString;
string activationCode = Guid.NewGuid().ToString();
using (SqlConnection sqlConn = new SqlConnection(constr))
{
using (SqlCommand sqlCom = new SqlCommand("INSERT INTO UserActivations VALUES(@UserId, @ActivationCode)"))
{
using (SqlDataAdapter sqlAdpt = new SqlDataAdapter())
{
sqlCom.Parameters.AddWithValue("@UserId", userId);
sqlCom.Parameters.AddWithValue("@ActivationCode", activationCode);
sqlCom.Connection = sqlConn;
sqlConn.Open();
sqlConn.Close();
}
}
}
using (MailMessage mm = new MailMessage("sender@gmail.com", txt_mail.Text))
{
mm.Subject = "Activation Mail";
string body = "Hello " + txt_username.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("lukagamermaker@gmail.com", "1,2,3,4,5,6");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Activatio mail sent , please check you e-mail.');", true);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
sqlCon.Open();
string checkuser = "select count(*) from UsersInfo where UserName='" + txt_username.Text + "'";
SqlCommand sqlCom = new SqlCommand(checkuser,sqlCon);
int temp = Convert.ToInt32(sqlCom.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("There is already user using that User Name");
}
sqlCon.Close();
}
}
protected void btn_register_Click(object sender, EventArgs e)
{
int userid = 0;
if ((txt_username.Text != "") && (txt_password.Text != "") && (txt_mail.Text != ""))
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["UsersConnectionString"].ConnectionString);
sqlCon.Open();
string insertQuery = "insert into UsersInfo (UserName,Password,Mail) values (@Uname , @Pass , @Email)";
SqlCommand sqlCom = new SqlCommand(insertQuery, sqlCon);
sqlCom.Parameters.AddWithValue("@Uname", txt_username.Text);
sqlCom.Parameters.AddWithValue("@Pass", txt_password.Text);
sqlCom.Parameters.AddWithValue("@Email", txt_mail.Text);
ActivationMail(userid);
Response.Redirect("Login.aspx");
sqlCon.Close();
}
else
{
Response.Write("Error in making account");
}
}
}
答案 0 :(得分:0)
您没有执行插入查询。使用ExecuteNonQuery()
SqlCommand.ExecuteNonQuery方法:对连接执行Transact-SQL语句并返回受影响的行数。
更新您的代码以执行您的查询
using (SqlCommand sqlCom = new SqlCommand("INSERT INTO UserActivations VALUES(@UserId, @ActivationCode)"))
{
sqlCom.Parameters.Add("@UserId",SqlDbType.VarChar,30).Value=userId;
sqlCom.Parameters.Add("@ActivationCode",SqlDbType.VarChar,30).Value=activationCode;
sqlCom.Connection = sqlConn;
sqlConn.Open();
//here it is
sqlCom.ExecuteNonQuery();
sqlConn.Close();
}
使用Parameters.AddWithValue是个坏主意。在这种情况下,ADO.NET必须猜测数据类型,并且在使用字符串和AddWithValue时存在特殊危险。 有关详细信息,
读 Under the Table - How Data Access Code Affects Database Performance Can we stop using AddWithValue() already?(摘自@mason的评论)