简体   繁体   English

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

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

I want to access the filled Dataset variable which contains one row data from user table, in my form2(coordinator2). 我想在我的form2(coordinator2)中访问填充的Dataset变量,该变量包含来自用户表的一行数据。 I serached a lot, but didn't understand the methods some people describing. 我学习了很多东西,但是不了解某些人描述的方法。 here is my code: 这是我的代码:

            // 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);
        }

As you see, in the case "2", I want to send the "ds" to coordinator2 form, so that I will be able to welcome the logged user and also filter data repeater rows according to logged user specifications. 如您所见,在“ 2”的情况下,我想将“ ds”发送到coordinator2表单,这样我就可以欢迎登录的用户,并根据登录的用户规范过滤数据转发器行。 any help will be appreciated. 任何帮助将不胜感激。

You could create a class variable in Coordinator2 and set it's value using this class's constructor like below: 您可以在Coordinator2创建一个类变量,并使用该类的构造函数设置其值,如下所示:

public class Coordinator2 
{
    private DataSet _ds = null;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM