c# - 无法在 C# 中将行转换为 DataTable

标签 c# database datatable datarow

我正在尝试将 DataRow 转换为 DataTable,但出现错误。我搜索并尝试了所有可能的解决方案,但没有一个奏效!

我有一个方法接受一个DataTable作为它的参数(这个DataTable只有一行)。该方法将返回一些信息。

起初,我尝试使用 ImportRow(newtable.ImportRow(row))DataRow 转换为 DataTable,但是 newtable 之后是空的。然后,我尝试了 dt.clone(),但这会用所有内容填充 newtable,这不是我想要的!实际上正是我所追求的。

private static void BatchFrontSidePrinting(Student St, frmBaseCard frm)
{
    DBINFOPACK UserInfo;
    DBCARDINFO CardInfo;
    DataTable newtable = new DataTable("newtable");

    foreach (DataRow row in dt.Rows)
    {
        try
        {

            // here, I'm trying to send one DataRow as a DataTable to the GetInfo() method,
            // and for the next iteratio , after getting the info I'm removing the row which was just added,
            // so that for the next iteration, newdatatable is empty.  All of the proceeding actions fail !:( 

            newtable.ImportRow(row); // doesn't work! 

            UserInfo = GetInfo(newtable);

            newtable.Rows.Remove(row); // doesn't work! 

            St = UserInfo.STUDENT;

            ((frmFrontSideCard)frm).Replica = UserInfo.CARDINFO.Replica;

            if (UserInfo.CARDINFO.Replica)
            {
                Loger.Items.Add("Replication !");
            }

            // print
            ((frmFrontSideCard)frm).Print = St;

            // update
            CardInfo = UserInfo.CARDINFO;
            CardInfo.SID = UserInfo.STUDENT.ID;
            CardInfo.BIMAGE = UserInfo.BIMAGE;
            SetInfo(CardInfo);
        }
        catch (Exception exep)
        {
            Loger.Items.Add(String.Format("Inside [BatchFrontSidePrinting()] : Student {0} {1}:", St.ID, exep.Message));
        }
    }
}

最佳答案

foreach (DataRow row in dt.Rows)
{
    try
    {
        DataTable newtable = new DataTable();
        newtable = dt.Clone(); // Use Clone method to copy the table structure (Schema).
        newtable.ImportRow(row); // Use the ImportRow method to copy from dt table to its clone.
        UserInfo = GetInfo(newtable);

    catch (Exception exep)
    {
        //
    }
}

关于c# - 无法在 C# 中将行转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621464/

相关文章:

c# - 如何从 Unity 3d Shaderlab 中的像素着色器返回纹理?

c# - LINQ查询数据表,加入多个选择

c# - SQL C# 交互不起作用

javascript - 我想在 Asp.Net MVC 中使用 Ajax 显示表数据。这个 Ajax 语法有什么问题?

c# - 如何为方法创建命名 `params` 参数?

c# - 在 C# 中向 Notepad++ 发送消息

c# - 控制 KENDO UI 圆环图的大小

php - 可能的 PDOException 错误(MySQL 5)?

Java/JDBC MySQL 数据库 - 设计查询

sql - 如何比较 Oracle 中的复杂集合?