如何显示从窗体1到窗体2的数据

时间:2018-12-28 17:33:27

标签: c# sql-server database visual-studio

我想从数据库中的“ form 1”(名字,用户名和角色名)中选择3列,以将其显示在form 2上。 名字和用户名显示在表格2上,但没有出现角色名,我的代码是否有错误? 表格2中角色角色名称中的图片中的错误假定显示为“液体实验室用户” /“生食实验室用户”。

form1 and form 2

表格1

 namespace labuser
 {
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {               
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=AlcoholSystem;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter("select firstname,rolename from tblUser where username='" + username.Text + "' and password = '" + password.Text + "'", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);


        if (dt.Rows.Count == 1)
        {
            MessageBox.Show("Username and password is correct.");
            this.Hide();
            Form2 ss = new Form2(username.Text, dt.Rows[0][0].ToString());
            ss.Show();
        }
        else
        {
            MessageBox.Show("Please check your username and password!");
        }
    }

表格2

namespace labuser
{
   public partial class Form2 : Form
  {
    SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=AlcoholSystem;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    DataTable table = new DataTable();


    string[] ports = SerialPort.GetPortNames();

    public Form2(String Username, String firstname, String rolename)
    {

        InitializeComponent();
        label5.Text = Username;
        label8.Text = firstname;
        label13.Text = rolename;


        serialPort1.BaudRate = 9600;
    }

1 个答案:

答案 0 :(得分:0)

将角色名称作为第三个参数传递

Form2 ss = new Form2(username.Text, dt.Rows[0][0].ToString(), dt.Rows[0][1].ToString());