我是C#的新手,目前正在使用“帐户” Windows应用程序。我没有使用ASP.NET,仅使用Winforms和.mdf数据库。我现在的主要目标是在登录后立即在主页上显示用户的详细信息(来自数据库)。
例如:
登录页面
用户名:sandy@mail.com
密码:1234
主页
欢迎,桑迪·史密斯!
您当前的地址:Woodlands Drive
这是我的“登录”按钮的代码:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\sit\Desktop\25June_LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonAnalysis.mdf;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select count(*) from TBL_LOGIN where STAFF_EMAIL ='" + textBox1.Text + "' and PASSWORD='" + textBox2.Text + "'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Home_Page hp = new Home_Page();
hp.Show();
}
else
{
MessageBox.Show("Please a valid email and password.", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
如果有人可以帮助,将不胜感激。谢谢!
答案 0 :(得分:-1)
这就是我使用winforms
在数据库创建表USERS
中,其列为USERID
,PASS
,DISPLAY_NAME
,EMAIL
,ANYTHING YOU WANT TO STORE FOR EACH USER
......
创建新类(.cs file
)并将其命名为CurrentUser
在其中为数据库中的每一列创建public static vars
-可以从代码中的任何位置(写/读)访问的数据类型。
所以类看起来像这样:
public class CurrentUser
{
public static int userId;
public static string pass;
public static string displayName;
public static string email;
//every other column you added
}
现在创建这样的登录代码
private void button1_Click(object sender, EventArgs e)
{
using(SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\sit\Desktop\25June_LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonAnalysis.mdf;Integrated Security=True");
{
conn.Open();
using(SqlCommand cmd = new SqlCommand("SELECT PASS, USERID, DISPLAY_NAME, FROM USERS WHERE EMAIL = @MAIL", conn)
{
cmd.Parameters.AddWithValue("@MAIL", textBox1.Text);
SqlDataReader dr = cmd.ExecuteReader():
if(dr.Read()) //We do IF since we only need first one
{
//Now we check if typed password is equal to password from database
if(dr[0].ToString().Equals(textBox2.Text))
{
this.Hide();
Home_Page hp = new Home_Page();
hp.Show();
//Here we store current user data
CurrentUser.userId = Convert.ToInt32(dr[1]);
CurrentUser.pass = dr[0].ToString();
CurrentUser.displayName = dr[2].ToString();
CurrentUser.email = textBox1.Text;
}
else //Passwords doesn't match
{
MessageBox.Show("Wrong password. Try again");
}
}
else //If it hasn't found anyone with that email that mean email is incorect
{
MessageBox.Show("E-mail doesn't exist!");
}
}
}
}
现在,无论您属于哪种类/形式(位于名称空间中),都可以使用string displayName = CurrentUser.displayName
访问当前用户的数据。
或者如果您想设置带有问候消息的标签,则可以
label1.Text = "Welcome, " + CurrentUser.displayName + "!";
更好
label1.Text = String.Format("Welcome, {0}!", CurrentUser.displayName);
或者,如果您想显示包含所有数据的messageBox,请这样做
string[] msg = {
String.Format("Welcome, {0}.", CurrentUser.displayName),
String.Format("Your ID is: {0} while your e-mail is {1}!", CurrentUser.userId, CurrentUser.email)
};
MessageBox.Show(String.Joing(Environment.NewLine, msg));