将数据集转换为数据表 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12627066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:56:43  来源:igfitidea点击:

convert dataset to datatable c#

c#datatabledataset

提问by ayou

ds.Tables.Add(dt);
da = new SqlDataAdapter(
  @"select Time1, Time2, EndDate from Event 
    where Venue ='" + txtVenue.Text + "', 
    StartDate ='" + cbStartMonth.Text + "/" + 
    cbStartDay.Text + "/" + 
    DateTime.Today.Year + "'" ,conn);
da.Fill(dt);

i'm sorry for the confusion, the code actually works but the problem is now how to view data coming from the data table

我很抱歉造成混乱,代码实际上有效,但现在的问题是如何查看来自数据表的数据

回答by Aghilas Yakoub

Just you adjust with .Tables property

只要你调整 .Tables property

var result = yourDataSet.Tables[0];

Your table is empty because you can adjust your query with and between clauses

您的表是空的,因为您可以使用子句和子句之间调整您的查询

But you rewrite your query ( You add and operator)

但是你重写你的查询 ( You add and operator)

da = new SqlDataAdapter("select Time1, Time2, EndDate from Event 
where Venue ='" + txtVenue.Text + "' AND 
StartDate ='" + cbStartMonth.Text + "/" + 
cbStartDay.Text + "/" + 
DateTime.Today.Year + "'" ,conn);

回答by Dave Zych

A DataSetis a collection of DataTables. You don't convert from one to the other, one stores the other.

ADataSetDataTables的集合。你不会从一个转换到另一个,一个存储另一个。

If only 1 row is showing up, then your Sqlis only returning a single row.

如果只显示 1 行,那么您Sql只返回单行。

回答by SolidRegardless

I would also change your code to use parameters. Otherwise you are leaving yourself open for serious SQL injection attacks all over the place.

我也会更改您的代码以使用参数。否则,您就会到处遭受严重的 SQL 注入攻击。

Look here for an example of how to use parameters with your SqlDataAdapter. http://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.71).aspx

在此处查看有关如何在 SqlDataAdapter 中使用参数的示例。 http://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.71).aspx

It also answers your question.

它也回答了你的问题。

HTH SR

HTH SR

回答by Suresh klt

you can use

您可以使用

ds.Table.Add(dt);
    DataTable dt1=ds.Tables[0];