首页 > 解决方案 > 使用来自用户登录的数据库信息填充公共字符串

问题描述

我创建了一个类,该类将用户详细信息从 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();
        }
    }

标签: c#

解决方案


用这种方法试试

cmd.Parameters.Add(@"@technr", SqlDbType.Int).Value = myI.technr;

推荐阅读