【发布时间】:2014-12-14 03:21:02
【问题描述】:
下面是我的代码。我正在尝试将一些数据显示为来自 SQL 服务器的数组图。问题是当我到达foreach 时,我似乎无法让它正确显示数据,它要么不断重复数据,要么就是不显示它。如果这个问题看起来很基本,我深表歉意帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string connection = "Data Source=INIWS64-04;Initial Catalog=INEW18_2013;Network Library=DBMSSOCN; User ID=recordsadmin;Password=security";
DataTable table = new DataTable();
string Sql = "";
Sql = "select * from qryEDocRequest order by Request_ID";
using (SqlConnection conn = new SqlConnection(connection))
{
try
{
conn.Open();
SqlCommand comm = new SqlCommand(Sql, conn);
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.Fill(table);
}
for (int i = 0; i < table.Rows.Count; ++i)
{
DataRow row = table.Rows[i];
for (int w = 0; w < table.Columns.Count; ++w)
{
StringBuilder array = new StringBuilder();
foreach (Char Item in row.ItemArray.ToString())
{
array.Append(Item + ",");
}
Console.WriteLine(array);
}
}
}
catch (Exception Test)
{
throw Test;
}
}
}
}
}
【问题讨论】:
-
for (char l = 0;.....的其余部分在哪里? -
哎呀我输入了错误的代码我有一个 foreach foreach (Char Item in row.ItemArray.ToString()) { array.Append(Item + ","); } Console.WriteLine(array);
-
我用正确的更新了它......当我决定问这个问题时,我正在尝试另一个 for 循环......对此感到抱歉
标签: c# arrays loops for-loop foreach