c# - 在 WPF App 中根据用户名显示用户的 SQL 信息
问题描述
我正在创建一个 WPF 应用程序,用户创建一个帐户并可以使用他们的用户名和密码登录。当用户成功登录他们的用户名时,他们在注册时输入的其他详细信息应显示在下一页上。到目前为止,当我这样做时,唯一显示的是第一个注册用户的信息,无论使用什么用户名或密码,但它应该基于谁登录。
更好地解释,有用户 A 和用户 B,当用户显示A登录他的信息,当用户B登录时,无论如何仍然显示用户A的信息,我希望用户B(以及所有后续用户)的信息在输入他的特定用户名时显示。
用于注册命令的 C#
private void SubmitBtn_Click(object sender, RoutedEventArgs e)
{
if (tbStudentName.Text == "" || pbPassword.Password == "" || tbSchoolName.Text == "" || tbHouseName.Text == ""
|| tbProg.Text == "" || tbPhoneNumber.Text == "" || tbAddress.Text == "")
{
var dim = new Dim();
dim.Show();
this.Effect = new BlurEffect();
var cmb = new Custom_MessageBoxes.CustomMsgBox2();
cmb.ShowDialog();
this.Effect = null;
dim.Close();
}
else
{
Connect obj = new Connect();
obj.conn.ConnectionString = obj.locate;
obj.conn.Open();
string InsertUser = "INSERT INTO tblSignUp values ('"+tbStudentName.Text+ "', '" + tbSchoolName.Text + "', '" + tbHouseName.Text + "', '" + tbProg.Text + "', '" + tbPhoneNumber.Text + "', '" + tbAddress.Text + "', '" + pbPassword.Password + "')";
obj.cmd.Connection = obj.conn;
obj.cmd.CommandText = InsertUser;
obj.cmd.ExecuteNonQuery();
obj.conn.Close();
var dim = new Dim();
dim.Show();
this.Effect = new BlurEffect();
var cmb = new Custom_MessageBoxes.RegistrationComplete();
cmb.ShowDialog();
this.Effect = null;
dim.Close();
Clear();
}
}
用于登录命令的 C#
//Sign In button click event
private void UserSignInBtn_Click(object sender, RoutedEventArgs e)
{
SqlConnection sqlCon = new SqlConnection(connectionString);
try
{
Connect obj = new Connect();
obj.conn.ConnectionString = obj.locate;
obj.conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT COUNT (*) FROM tblSignUp WHERE StudentName = '"+tbID.Text+"' AND Password = '"+PB.Password+"'", obj.conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
// Custom Message Box and Dim Effect
var jim = new Dim();
jim.Show();
this.Effect = new BlurEffect();
var lsmb = new Custom_MessageBoxes.LoginSuccessfulMsgBox();
lsmb.ShowDialog();
this.Effect = null;
jim.Close();
var User_Homepage = new User_Homepage();
NavigationService.Navigate(User_Homepage);
}
else
{
// Custom Message Box and Dim Effect 2
var him = new Dim();
him.Show();
this.Effect = new BlurEffect();
var rmdlgb = new ReturnMessageDialogueBox();
rmdlgb.ShowDialog();
this.Effect = null;
him.Close();
}
obj.conn.Close();
}
catch(Exception ex)
{
using (EventLog eventlog = new EventLog("Application"))
{
eventlog.Source = "SQL Error: From My Application";
eventlog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 101, 1);
}
}
finally
{
sqlCon.Close();
}
}
我想要用户信息的页面
string connectionString = @"Data Source=HP;Initial Catalog=User_SignUpDB;Integrated Security=True;";
public UHP()
{
InitializeComponent();
Page1 p1 = new Page1();
var pls = p1.tbID.Text;
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string query = "SELECT * FROM tblSignUP WHERE StudentName = StudentName and HouseName = HouseName";
SqlCommand createCommand = new SqlCommand(query, sqlCon);
SqlDataReader dr = createCommand.ExecuteReader();
if (dr.Read())
{
nameTxt.Text = (dr["StudentName"].ToString());
hseTxt.Text = (dr["HouseName"].ToString());
progTxt.Text = (dr["Prog"].ToString());
}
sqlCon.Close();
}
解决方案
您的查询:
SELECT *
FROM tblSignUP
WHERE
StudentName = StudentName
AND HouseName = HouseName
没有参数传递给它;这只是一个硬编码的语句。
您正在比较您的WHERE
子句中的等效字段,这使得它变得多余,即您实际上只是在SELECT *
从表中进行操作。因此,您在应用程序中读取的内容始终只是返回的第一行。
你需要的是这样的:
string query = "SELECT * FROM tblSignUP WHERE StudentName = @StudentName and HouseName = @HouseName";
SqlCommand createCommand = new SqlCommand(query, sqlCon);
createCommand.Parameters.Add(new SqlParameter("@StudentName", StudentName));
createCommand.Parameters.Add(new SqlParameter("@HouseName", HouseName));
传递给构造函数(第二个参数)的变量StudentName
,我假设已经在你的代码中的某个地方定义了。HouseName
SqlParameter
推荐阅读
- javascript - 单元测试错误:无法读取未定义的属性“地图”
- firebase - Firebase 索引在幕后有什么作用?
- jdbc - 我们可以在 Windows 操作系统中运行 Confluent JDBC Kafka 连接器吗?
- java - Android,JaCoco 无法为“android.uid.system”创建覆盖
- java - 在没有库的情况下在 Java 中读取 PKCS#1 或 SPKI 公钥
- c# - Node.js 中的 C# Rijndael 解密代码得到错误错误解密
- c - 如何在我的代码中修复系统调用 Ntcreateprocess 原型的未定义符号?
- c++ - C++ 覆盖 void 函数返回类型导致构建失败
- c++ - 如何在 C++03 中用 sprintf 正确替换 sprintf_s?
- amazon-web-services - 使用 Route53 资源处理 Terraform 异常