我一直坚持这个问题:我试图在某个条件下保存表(数据库)中的列的值。
在下面的代码中,我试图将文本框(sUserName)的输入与表(aspnet_Membership)中列(UserName)中的值进行比较。如果这些值相等,我想在列中获取特定的电子邮件值并将其保存为字符串变量。
如果UserName(列)不等于sUserName(文本框),那么我想显示错误消息(else语句)。 “电子邮件和用户名”列位于同一个表中
string sUserName = txtBoxUsername.Text;
SqlConnection conn2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\sunny\Visual Studio 2010\Projects\email\Prac 2\App_Data\aspnet_Membership.mdf;Integrated Security=True;User Instance=True");
SqlCommand myCommand = new SqlCommand("SELECT Email FROM aspnet_Membership WHERE UserName = sUserName", conn2);
答案 0 :(得分:0)
只需在表格中添加检查用户是否存在以下代码:
string sUserName = txtBoxUsername.Text;
SqlConnection conn2 = new SqlConnection("Your SQL Connection");
SqlCommand myCommand = new SqlCommand("SELECT Email FROM aspnet_Membership WHERE UserName = '"+ sUserName + "'", conn2);
SqlDataReader rdr = myCommand.ExecuteReader();
if (dr.HasRows)
{
while (rdr.Read())
{
// User exist - get email
string email = rdr["Email "].toString();
}
}
else
{
//Error! user not exist
}
最好的问候