[英]How to store the data from a datatable in an array
I am retrieving the data from a database table[110 rows and 8 columns] using a webservice and I want to store all this data in an array so that I can view and access this data easily on my android application. 我正在使用Web服务从数据库表(110行8列)中检索数据,我想将所有这些数据存储在一个数组中,以便可以在android应用程序上轻松查看和访问该数据。
this is my code: 这是我的代码:
[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();
}
I think you're kinda doing this the hard way. 我认为您有点很难。 Firstly, if you don't want the table in a
DataTable
: don't use a DataTable
. 首先,如果您不想在
DataTable
使用该表: 不要使用DataTable
。 You mention JSON in the code, so I'm going to assume this is meant to be idiomatic JSON; 您在代码中提到了JSON,因此我将假定这是惯用的JSON。 in which case - use a POCO:
在这种情况下-使用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;}
}
Then populate the data from the sproc - something like "dapper" would really help here: 然后从存储过程中填充数据-像“ dapper”之类的东西在这里确实有帮助:
List<CategoryWiseRow> data;
using(var con = new SqlConnection(....))
{
data = con.Query<CategoryWiseRow>("ReportSummery", new { district },
commandType: CommandType.StoredProcedure).ToList();
}
and use whatever JSON tool you prefer - for example Json.NET: 并使用您喜欢的任何JSON工具-例如Json.NET:
string json = JsonConvert.SerializeObject(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.