如何将一个DataSet转换成数组?

weddingin2008 2004-09-07 08:35:13
效果就像DataGrid.DataSource=ds一样能DataGrid.DataSource=数组名?
...全文
1016 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqcbmfc 2004-09-08
  • 打赏
  • 举报
回复
关注中
weddingin2008 2004-09-08
  • 打赏
  • 举报
回复
意义重大,我只要结果:
webdiyer 2004-09-08
  • 打赏
  • 举报
回复
把DataSet转换成一个二维数组再绑定到DataGrid上?楼上这样做有什么意义呢??
weddingin2008 2004-09-08
  • 打赏
  • 举报
回复
up
weddingin2008 2004-09-08
  • 打赏
  • 举报
回复
我要将DataSet中的一个表转换成一个二维数组后绑定效果和原来一样,怎么没有人知道啊?
悄悄的回来了 2004-09-08
  • 打赏
  • 举报
回复
SqlConnection conn=new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
conn.Open();
string strSQL="select au_lname,au_fname,address from authors";
DataRow drName;
DataTable dtStuUsers=new DataTable("authors");
DataSet dsStudent=new DataSet();
dsStudent.Tables.Add(dtStuUsers);
int intCount=3;
SqlDataReader sdrStudent;
SqlCommand cmdStudent;
//二维数组的行和列(SQL语句所选择的烈数)自己定义(我是根据表中的记录数来定的)
String[,] strText=new String[23,3];
int intI=0,intJ=0;
for(int i=0;i<intCount;i++)
{
//创建表中的列
dtStuUsers.Columns.Add(new DataColumn("列"+i.ToString(),typeof(string)));
}
//打开数据集
cmdStudent=new SqlCommand(strSQL,conn);
sdrStudent=cmdStudent.ExecuteReader();
//向内存表中添加数据
while(sdrStudent.Read())
{
drName=dtStuUsers.NewRow();
for(intI=0;intI<3;intI++)
{
strText[intJ,intI]=sdrStudent.GetString(intI).ToString();
//你可以将数组操作以后再将数值填充到 drName里,在绑到DataGrid里
drName[intI]=sdrStudent.GetString(intI);
}
intJ=intJ+1;
dtStuUsers.Rows.Add(drName);
}
for(intJ=0;intJ<23;intJ++)
{
for(intI=0;intI<3;intI++)
{
Response.Write(strText[intJ,intI]+"**");
}
Response.Write("<br>");
}
dgName.DataSource=dsStudent.Tables["authors"];
dgName.DataBind();
weddingin2008 2004-09-07
  • 打赏
  • 举报
回复
楼上的有错误:
System.NullRefrenceException:未将对象引用设置到对象的实例.
XiaoZhengGe 2004-09-07
  • 打赏
  • 举报
回复
private Array ArrayList_Northwind_customers()
{
DataSet ds=new DataSet();
ArrayList al=new ArrayList();
SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
SqlDataAdapter da=new SqlDataAdapter("select top 100 CustomerID,CompanyName,ContactName,ContactTitle from customers",conn);
conn.Open();
da.Fill(ds,"Northwind_customers");
object[][] str=null;
for(int i=0;i<ds.Tables["Northwind_customers"].Rows.Count;i++)
{
for(int j=0;j<ds.Tables["Northwind_customers"].Columns.Count;j++)
{
str[i][j]=ds.Tables["Northwind_customers"].Rows[i][j].ToString();
}
}
conn.Close();
return str;
}
weddingin2008 2004-09-07
  • 打赏
  • 举报
回复
我不要赋值,我要的是转换:
public ArrayList ArrayList_Northwind_customers()
{
DataSet ds=new DataSet();
ArrayList al=new ArrayList();
SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
SqlDataAdapter da=new SqlDataAdapter("select top 100 CustomerID,CompanyName,ContactName,ContactTitle from customers",conn);
conn.Open();
da.Fill(ds,"Northwind_customers");
foreach(DataRow row in ds.Tables["Northwind_customers"].Rows)
{
foreach(System.Data.DataColumn col in ds.Tables["Northwind_customers"].Columns)
{
al.Add(row[col].ToString());
}
}
conn.Close();
return al;
}
这样可以吗?
stan0714 2004-09-07
  • 打赏
  • 举报
回复
object[][] str=null;
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
for(int j=0;j<ds.Tables[0].Columns.Count;j++)
{
str[i][j]=ds.Tables[0].Rows[i][j].ToString();
}
}
大概就是这个意思吧!!!
webdiyer 2004-09-07
  • 打赏
  • 举报
回复
<script runat="server">

void Page_Load(object src,EventArgs e){
if(!Page.IsPostBack){
string[] names={"one","two","three","four","five","six","seven","eight"};
grid.DataSource=names;
grid.DataBind();
}


<asp:DataGrid runat="server" id="grid"/>
wangrenda 2004-09-07
  • 打赏
  • 举报
回复
学习
weddingin2008 2004-09-07
  • 打赏
  • 举报
回复
怎么写代码啊?
webdiyer 2004-09-07
  • 打赏
  • 举报
回复
本来就可以的,只要实现了IEnumerable接口就可以做为DataGrid的DataSource,数组都是实现这个接口的。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧