如何将数据表中的数据存储在数组中
本文关键字:存储 数组 数据 数据表 | 更新日期: 2023-09-27 18:20:13
我正在使用Web服务从数据库表[110行8列]中检索数据,我想将所有这些数据存储在一个数组中,以便在我的android应用程序上轻松查看和访问这些数据。
这是我的代码:
[WebMethod(Description = "Webservice for generating category wise report in xml")]
public string getCategoryWiseReport_2(string district)
{
string s = null;
string ofc_code=null;
string ofc_desg=null;
string ofc_name=null;
string dep_name=null;
string total_comp=null;
string pending=null;
string desposed=null;
string interim=null;
string defaulter=null;
string arr[];
var con = new SqlConnection("data source=xxxx;initial catalog=xyz;integrated security=true");
StringBuilder JSON = new StringBuilder();
con.Open();
var cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "ReportSummery";
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@district", district);
cmd.ExecuteNonQuery();
ad.Fill(dt);
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
ofc_code = dt.Rows[i][0].ToString();
ofc_desg = dt.Rows[i][1].ToString();
ofc_name = dt.Rows[i][2].ToString();
dep_name = dt.Rows[i][3].ToString();
total_comp = dt.Rows[i][4].ToString();
pending = dt.Rows[i][5].ToString();
desposed = dt.Rows[i][6].ToString();
interim = dt.Rows[i][7].ToString();
defaulter = dt.Rows[i][8].ToString();
}
return ofc_code + "," + ofc_desg + "," + ofc_name + "," + total_comp + "," + pending + "," + desposed + "," + interim + "," + defaulter;
con.Close();
}
我认为你这样做有点困难。首先,如果不希望表位于DataTable
:中,请不要使用DataTable
。您在代码中提到了JSON,所以我假设这是惯用的JSON;在这种情况下-使用POCO:
class CategoryWiseRow {
public string ofc_code {get;set;}
public string ofc_desg {get;set;}
public string ofc_name {get;set;}
public string dep_name {get;set;}
public string total_comp {get;set;}
public string pending {get;set;}
public string desposed {get;set;}
public string interim {get;set;}
public string defaulter {get;set;}
}
然后从存储过程中填充数据——类似于"dapper"的东西在这里会很有帮助:
List<CategoryWiseRow> data;
using(var con = new SqlConnection(....))
{
data = con.Query<CategoryWiseRow>("ReportSummery", new { district },
commandType: CommandType.StoredProcedure).ToList();
}
并使用您喜欢的任何JSON工具,例如JSON.NET:
string json = JsonConvert.SerializeObject(data);