C#遍历DataTable中的行

时间:2016-02-24 15:37:37

标签: c# datatable

我试图遍历2列表中的行,以针对名称检查每行中的1个字段。一旦找到,我想编码将相应的Number分配给OurNumber变量,并通过将GotTheNumber设置为true来突破循环。

以下是我使用的代码:

    private void BtnDelete_Click(object sender, EventArgs e)// Sends to ConfirmDeleteEMP Form 
    {            
        ConfirmDeleteEMP form = new ConfirmDeleteEMP();
        DataTable table = new DataTable();
        string connstring = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
        using (OleDbConnection conn = new OleDbConnection(connstring))
        {
            string query = "SELECT PayrollNo, (FirstName + ' ' + LastName) AS NAME FROM [Employee]";                 
            OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
            adapter.Fill(table);                                        
        }

        string SelectedName = DropBoxEmp.Text;            
        bool GotTheNumber = false;
        int OurNumber = 0;
        while (!GotTheNumber)
        {
            foreach (DataRow ThisRow in table.Rows)
            {
                if (SelectedName = (table.Rows[ThisRow])) 
                {
                    OurNumber = ///THATNUMBER///;
                    GotTheNumber = true;
                }  
            }
        }

        MessageBox.Show(SelectedName);
        var GoodNumber = (table.Rows[OurNumber]["PayrollNo"].ToString());
        form.PassValueName = SelectedName;
        form.PassSelectedPayroll = GoodNumber;               
        form.Tag = this;
        form.Show(this);
        Hide();
    }

我不知道从If语句中去哪里,所以非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

循环播放客户端程序中的行正是想要做的事情。让数据库为您完成这项工作。试试这个:

private void BtnDelete_Click(object sender, EventArgs e)// Sends to ConfirmDeleteEMP Form 
{     
    object result;       
    string query = "SELECT PayrollNo FROM [Employee] WHERE FirstName + ' ' + LastName = ?"; 
    string connstring = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";

    using (OleDbConnection conn = new OleDbConnection(connstring))
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
        //guessing at type and length here
        cmd.Parameters.Add("?", OleDbType.VarWChar, 50).Value = DropBoxEmp.Text;

        conn.Open();
        result = cmd.ExecuteScalar();                                        
    }

    if (result != null && result != DBNull.Value)
    {
        ConfirmDeleteEMP form = new ConfirmDeleteEMP();
        form.PassValueName = DropBoxEmp.Text;
        form.PassSelectedPayroll = (int)result;
        form.Tag = this;

        form.Show(this);
        Hide();
    }                    
}

如果确实想要在所有原因中循环遍历行(它更慢,需要编写更多代码,并且更容易出错),您可以这样做:

private void BtnDelete_Click(object sender, EventArgs e)// Sends to ConfirmDeleteEMP Form 
{                   
    DataTable table = new DataTable();
    string query = "SELECT PayrollNo, (FirstName + ' ' + LastName) AS NAME FROM [Employee]"; 
    string connstring = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
    using (OleDbConnection conn = new OleDbConnection(connstring))
    {                    
        OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
        adapter.Fill(table);                                        
    }

    int PayrollNumber = 0;
    foreach (DataRow ThisRow in table.Rows)
    {
        if (DropBoxEmp.Text == ThisRow["NAME"])
        {
            PayrollNumber = (int)ThisRow["PayrollNo"];
            break;
        }  
    }
    //the whole loop could also be consolidated to this:
    //PayrollNumber = (int)table.Rows.First(r => r["NAME"] == DropBoxEmp.Text)["PayrollNo"];

    ConfirmDeleteEMP form = new ConfirmDeleteEMP();
    form.PassValueName = DropBoxEmp.Text;
    form.PassSelectedPayroll = PayrollNumber ;               
    form.Tag = this;
    form.Show(this);
    Hide();
}

答案 1 :(得分:0)

嗯,很难猜出你的问题究竟是什么。但我想你只想从当前行获得PayrollNo,不是吗?

关于进一步向下的线......

var GoodNumber = (table.Rows[OurNumber]["PayrollNo"].ToString());

......我想你可以打电话:

if (...)
{
    OurNumber = ThisRow["PayrollNo"].ToString();
    GotTheNumber = true;
}

然而,我不知道你在使用以下if条件做了什么,如果这真的做了你想做的事情:

if (SelectedName = (table.Rows[ThisRow]))
{
...
}