如何将数据集值传递给数组 [英] how to pass dataset value into array
问题描述
如何将数据集值传递给数组。我的代码附加在此代码foreach(table.rows中的DataRow dr)此行显示错误 System.Data.DataSet
'不包含'行'和'的定义没有扩展方法'Rows'接受类型' System.Data.DataSet
'的第一个参数可以找到(你是否缺少using指令或程序集引用?)
How to pass dataset value into array . my code attached here in this code foreach (DataRow dr in table.rows) this line showing error"System.Data.DataSet
' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Data.DataSet
' could be found (are you missing a using directive or an assembly reference?) "
SqlCommand cmd = new SqlCommand("select price from Dataupload",sc );
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
string[] Gh = new string[ds.Tables[0].Rows.Count];
foreach (DataSet table in ds.Tables)
{
int i = 0;
foreach (DataRow dr in table.rows)
{
Gh[i] = dr["price"].ToString();
i++;
}
推荐答案
//try here eg:
//Empty array
string[] arrvalues = new string[ds.Tables[0].Rows.Count];
//loopcounter
for (int loopcounter = 0; loopcounter< ds.Tables[0].Rows.Count; loopcounter++)
{
//assign dataset values to array
arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["urcolumnName"].ToString();
}
试试这个.........
try this.........
/ Declaring Array
string[,] ArrayValues=new string[ DataTable.Rows.Count,datatable1.Columns.Count];
for(int row = 0; row < DataTable.Rows.Count; ++row)
{
for(int col = 0; col < DataTable.Columns.Count; col++)
{
// inserting Array values from DataTable
ArrayValues[row, col] = DataTable.Rows[row][col].ToString();
}
}
嗯......看一下错误信息:
Um... look at the error message:
System.Data.DataSet' does not contain a definition for 'Rows'
然后查看您的代码:
And then look at your code:
foreach (DataSet table in ds.Tables)
...
foreach (DataRow dr in table.rows)
...
甚至忽略C#区分大小写,因此行与行不同为什么您希望DataSet中的DataTable集合返回DataSet而不是DataTable?
尝试:
Even ignoring that C# is case sensitive, so "rows" is not the same as "Rows" why do you expect a collection of DataTables in a DataSet to return a DataSet rather than a DataTable?
Try:
foreach (DataTable table in ds.Tables)
并更正案例。
和BTW:这很危险。
And correct the case.
And BTW: this is dangerous.
string[] Gh = new string[ds.Tables[0].Rows.Count];
因为没有什么可以保证DataSet中的每个表具有相同的行数。
我建议您填写单个DataTable而不是填充DataSet而只使用一个环。它更安全,更准确地说明了你要做的事情。
Since there is nothing which guarantees that each table in the DataSet has the same number of rows.
I'd recommend that instead of filling a DataSet you fill a single DataTable and only use one loop. It's a lot safer, and more obvious exactly what you are trying to do.
这篇关于如何将数据集值传递给数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!