当executereader从表中读取行时,如何将行添加到数据表中?

发布于 2024-10-04 12:52:09 字数 44 浏览 6 评论 0原文

我想使用 C# 编码将executereader 读取的行添加到新数据表中

I want to add the rows that are read by executereader to the new datatable with C# coding

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2024-10-11 12:52:09

你的问题中有太多未知数。例如,数据表是类型化的还是非类型化的?列类型有哪些?等等。

但无论如何,这是一个一般的例子。我根据此处给出的原始示例改编了代码。

private static void ReadOrderData(string connectionString)
{
字符串查询字符串 =
“从 dbo.Orders 中选择订单 ID、客户 ID;”;

using (SqlConnection connection =
           new SqlConnection(connectionString))
{
    SqlCommand command =
        new SqlCommand(queryString, connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    // Call Read before accessing data.
    while (reader.Read())
    {
       CustomersRow newCustomersRow = Customers.NewCustomersRow();

       newCustomersRow.CustomerID = reader[0].ToString();
       newCustomersRow.CompanyName = reader[1].ToString();
       dt.Rows.Add(newCustomersRow);
    }

    // Call Close when done reading.
    reader.Close();
}

}

There are too many unknowns in your question. Foreg, is the data table typed or untyped? What are the column types? And so forth.

But anyway, here's a general example. I adapted the code from the original sample given here.

private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";

using (SqlConnection connection =
           new SqlConnection(connectionString))
{
    SqlCommand command =
        new SqlCommand(queryString, connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    // Call Read before accessing data.
    while (reader.Read())
    {
       CustomersRow newCustomersRow = Customers.NewCustomersRow();

       newCustomersRow.CustomerID = reader[0].ToString();
       newCustomersRow.CompanyName = reader[1].ToString();
       dt.Rows.Add(newCustomersRow);
    }

    // Call Close when done reading.
    reader.Close();
}

}

千と千尋 2024-10-11 12:52:09

创建一个 dataTable 对象,其列与 datareader 的列相同。请看下面的代码。

例如:

DataTable _dt=new DataTable();//public 

void AddColumns()
{
 _dt.Columns.Add(new DataColumn("ID", typeof(int)));
 _dt.Columns.Add(new DataColumn("mark", typeof(int)));
}

所以在你的数据表中你有两列[ID和标记]。

检索数据

System.Data.SqlClient.SqlDataReader rd=cmd.Execute
Reader();

编写一个函数来添加新行

public void NewRow(DataReader rd)
{
   while(rd.Read())
    {
   DataRow dr=_dt.NewRow();
   dr["ID"]=Convert.toInt32(rd["ID"]);
   dr["Mark"]=Convert.toInt32(rd["Mark"]);
   _dt.Rows.Add(dr)
      }
}

这样您就可以向数据表添加值。

Make a dataTable oject with the columns same as that of the datareader. Pls have a look at the code below.

Eg:

DataTable _dt=new DataTable();//public 

void AddColumns()
{
 _dt.Columns.Add(new DataColumn("ID", typeof(int)));
 _dt.Columns.Add(new DataColumn("mark", typeof(int)));
}

So in your datatable u have two columns[ID and mark].

retriving data

System.Data.SqlClient.SqlDataReader rd=cmd.Execute
Reader();

Write a function to add New Row

public void NewRow(DataReader rd)
{
   while(rd.Read())
    {
   DataRow dr=_dt.NewRow();
   dr["ID"]=Convert.toInt32(rd["ID"]);
   dr["Mark"]=Convert.toInt32(rd["Mark"]);
   _dt.Rows.Add(dr)
      }
}

This way u can able to add values to dataTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文