c# - .net framework 3.5 登录表单代码不起作用
问题描述
我最近在 .net framework 4.7.2 中开发了一个 pos 系统,该应用程序运行良好并安装在客户端机器中。目前我正在为.net framework 3.5 中的另一个客户端开发管理系统我创建了数据库并开始编写登录表单,我从我的 pos 系统中获取现有代码更改了连接字符串一切都很顺利但是当我点击登录按钮没有任何反应,下一个表单不显示。这是代码
if (textBox1.Text == "")
{
MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox2.Focus();
return;
}
string cs = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=filali;Integrated Security=True";
SqlConnection myConnection = default(SqlConnection);
myConnection = new SqlConnection(cs);
SqlCommand myCommand = default(SqlCommand);
myCommand = new SqlCommand("select * from dbo.[login] where [username] = @user and password = @pass", myConnection);
SqlParameter uName = new SqlParameter("@user", SqlDbType.VarChar);
SqlParameter uPassword = new SqlParameter("@pass", SqlDbType.VarChar);
uName.Value = textBox1.Text;
uPassword.Value = textBox2.Text;
myCommand.Parameters.Add(uName);
myCommand.Parameters.Add(uPassword);
myCommand.Connection.Open();
try
{
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myReader.Read() == true)
{
string role = myReader.GetString(3).ToString();
Console.WriteLine(myReader.GetString(3).ToString());
switch (role)
{
case "a":
Console.WriteLine("this is admin "+role);
mdiparent1 mdiparent1 = new mdiparent1();
this.Hide();
mdiparent1.Show();
break;
case "u":
sortiev2 instance = new sortiev2();
this.Hide();
instance.Show();
if (instance.FormBorderStyle == FormBorderStyle.None)
instance.FormBorderStyle = FormBorderStyle.Sizable;
instance.BringToFront();
if (instance.WindowState == FormWindowState.Maximized)
instance.WindowState = FormWindowState.Normal;
break;
}
}
else
{
MessageBox.Show("Invalid username or password");
}
if (myConnection.State == ConnectionState.Open)
{
myConnection.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
sql 连接正在工作,因为“角色”记录在控制台中并且不是空的我不知道为什么它不执行 switch 语句是否有任何语法错误?是不是我创建了一个具有不同框架的项目?
解决方案
问题实际上是我的数据源,即返回尾随空格。
string role = myReader.GetString(3).ToString()
没有回来
a
但
'a '
为了解决这个问题,我只是添加.TrimEnd()
以摆脱尾随
而不是这个
string role = myReader.GetString(3).ToString()
我用这个:
string role = myReader.GetString(3).TrimEnd()
感谢所有帮助过的人!
推荐阅读
- angular - Angular 单元测试失败:没有 WebSocketAPI 的提供者
- regex - PostgreSQL查询中字符串最后n个字符的正则表达式
- python - 使用“或”的两种方式之间的区别
- php - Ubuntu/PHP - WebSocket 服务器不接受多个连接
- python - json.dumps 将对象作为字符串而不是 json 传递
- javascript - 我们如何从脚本更改类名
- javascript - Angular Electron & AudioWorklet.addModule() error DOMException: The user aborted a request
- c# - 为非活动代码区域启用智能感知
- python - Kafka python客户端在分配和重新平衡之间交替,不处理数据
- laravel - 在 Laravel 的 http 客户端异常之后做某事