如何在 C# 中将 select 查询的返回行发送到 form2?

How to send the returned row of select query to form2 in C#?

我想在我的 form2(coordinator2) 中访问包含来自用户 table 的一行数据的填充数据集变量。我搜索了很多,但不理解某些人描述的方法。这是我的代码:

            // main login connection with sql server part
            connectionString cs = new connectionString();
            SqlConnection sqlcon = new SqlConnection(cs.sqlstr);
            sqlcon.Open();
            SqlCommand sqlcomm = new SqlCommand("select * from [User Info], [Department Info],[Position Infor] where [User Name]= '" + usertb.Text + "' and Password = '" + userpass.Text + "' and [User Info].Department = [Department Info].DID and [User Info].Designation=[Position Infor].PosID;", sqlcon);
            SqlDataReader myreader;
            myreader = sqlcomm.ExecuteReader();
            int count = 0;
            while (myreader.Read())
            {
                count += 1;
            }
            myreader.Close();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = sqlcomm;
            da.Fill(ds);
            if (count == 1)
            {
                //this "if" condition returens the Full Name of selected user
                string fullname = ds.Tables[0].Rows[0][2].ToString();
                string dept = ds.Tables[0].Rows[0][9].ToString();
                string usercat = ds.Tables[0].Rows[0][7].ToString();
                MessageBox.Show("Welcome Dear " + fullname + " from " + dept + " to Task Management System");

                switch (usercat)
                {
                    case "1":
                        Monitoring_and_Evaluation mne = new Monitoring_and_Evaluation();
                        mne.ShowDialog();
                        break;
                    case "2":
                        Coordinator2 cr = new Coordinator2();
                        cr.ShowDialog();
                        break;
                    case "3":
                        Employee em = new Employee();
                        em.ShowDialog();
                        break;
                    case "4":
                        AdminSwitchForm asf = new AdminSwitchForm();
                        tasknotify.ShowBalloonTip(1, "User Welcome", "Hi Dear " + fullname, 0);
                        asf.ShowDialog();
                        break;
                    default:
                        MessageBox.Show("Dear" + fullname + "You don't have any right to login, please contact your System Admin");
                        break;
                }
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate");
            }
            else
            {
                MessageBox.Show("incorrect user name and password");
            }
            sqlcon.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

如您所见,在“2”的情况下,我想将 "ds" 发送到 coordinator2 表单,这样我就可以欢迎已登录的用户并根据已登录的用户过滤数据转发器行用户规范。任何帮助将不胜感激。

您可以在 Coordinator2 中创建一个 class 变量并使用此 class 的构造函数设置它的值,如下所示:

public class Coordinator2 
{
    private DataSet _ds = null;

    public Coordinator2(DatatSet ds)
    {
        InitializeComponent();
        _ds = ds;
    }
}