c# - 使用来自用户登录的数据库信息填充公共字符串
问题描述
我创建了一个类,该类将用户详细信息从 DB 获取到字符串(用户名、用户密码、usernr、usersubco)。我想要发生的是这些字符串填充了我登录用户的信息。
public class Details
{
public connectionUser constring = new connectionUser();
public string Technr;
public string Techpass;
public string Techname;
public string Techsubco;
public Details()
{
using (SqlConnection con = new SqlConnection(constring.source))
{
myInlogForm myI = new myInlogForm();
con.Open();
SqlCommand cmd = new SqlCommand("select * from techs where technr=@technr", con);
cmd.Parameters.Add(@"technr", SqlDbType.Int).Value = myI.technr;
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Technr = reader[0].ToString();
Techpass = reader[1].ToString();
Techname = reader[2].ToString();
Techsubco = reader[3].ToString();
break;
}
con.Close();
}
MessageBox.Show(Techname + Technr + Techpass + Techsubco);
}
myI = usercontrol (inlogscreen)
myIn.technummer
是一个从文本框中获取登录按钮值的公共字符串。
当我推送登录时,我从上面的这个类中得到了消息框(空)并出现以下错误:
更新 !!!
System.Data.SqlClient.SqlException:“参数化查询 '(@technr int)select * from techs where technr=@technr' 需要参数 '@technr',但未提供该参数。”
public event EventHandler<EventArgs> callMenu; // oproep door mainform
public event EventHandler<EventArgs> callDash; // oproep door mainform
public int technr;
private void loginBtn_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(cUser.source.ToString()))
{
con.Open();
SqlCommand cmd = new SqlCommand("select technr,techcode from techs where technr=@technr and techcode=@techcode", con);
cmd.Parameters.Add(@"technr",SqlDbType.Int).Value = userTxb.Text;
cmd.Parameters.Add(@"techcode",SqlDbType.VarChar).Value = passTxb.Text;
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0 && callMenu != null && callDash != null) // checks login is juist
{
callMenu(this, new EventArgs());
callDash(this, new EventArgs());
technr = int.Parse(userTxb.Text);
}
else
{
MessageBox.Show("Foutieve gegevens");
}
con.Close();
}
}
解决方案
用这种方法试试
cmd.Parameters.Add(@"@technr", SqlDbType.Int).Value = myI.technr;
推荐阅读
- objective-c - 为什么我不能在 XPC 服务中为 NSURLSessionDataTask 覆盖 SSL ServerTrust?
- php - 未定义的偏移量:2 和未定义的偏移量:1 和未初始化的字符串偏移量:0
- python - 如何使用 python selenium 在 for 循环中打开多个 chromedrivers?
- elasticsearch - FileBeat:仅主机字段显示为 JSON,而不是字符串
- conv-neural-network - 如何在keras中导入CNN模型
- c# - 当我从另一个窗口更改文本框值时如何更改它
- angular - 使用枚举分配的变量的正确类型定义
- java - 在Android应用程序中将Json转换为Csv格式
- flutter - Flutter 屏幕,在可绘制的屏幕左侧包含一个可滚动和可点击的磁贴
- c++ - 使用 LLVM LegacyPassManager 获取分析结果