下面的代码工作正常,但我想知道一种方法来拉出列标题吗?如果可能的话,我希望在我的while循环中。希望有人可以提供帮助。
谢谢,詹姆斯。
namespace WorldViewNet.include
{
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
object data = LoadAPI(context);
string jsonData = JsonConvert.SerializeObject(data);
context.Response.Write(jsonData);
}
public List<object[]> LoadAPI(HttpContext context)
{
List<object[]> list = new List<object[]>();
SqlConnection sqlConnection1 = new SqlConnection(globalLibrary.NewConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string tablename = context.Request.QueryString["tablename"];
string maybeID = context.Request.QueryString["ID"];
cmd.CommandText = "SELECT * FROM " + tablename;
if (maybeID != null)
{
cmd.CommandText += " WHERE " + tablename + "ID =" + maybeID;
}
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
object[] row = new object[23];
reader.GetValues(row);
list.Add(row);
}
sqlConnection1.Close();
return list;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
答案 0 :(得分:0)
你应该能够做到这一点:
reader = cmd.ExecuteReader();
var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();
columns
现在应该是所有列名的列表。
如果您需要非Linq方式来执行此操作:
reader = cmd.ExecuteReader();
var columns = new List<string>();
for(int z=0;z<reader.FieldCount;z++)
{
columns.Add(reader.GetName(z));
}