Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据表中的数据存储在数组中_C#_Android_Sql Server - Fatal编程技术网

C# 如何将数据表中的数据存储在数组中

C# 如何将数据表中的数据存储在数组中,c#,android,sql-server,C#,Android,Sql Server,我正在使用webservice从数据库表[110行8列]检索数据,我希望将所有这些数据存储在一个数组中,以便在android应用程序上轻松查看和访问这些数据 这是我的代码: [WebMethod(Description = "Webservice for generating category wise report in xml")] public string getCategoryWiseReport_2(string district) { string s = null;

我正在使用webservice从数据库表[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();
}
[WebMethod(Description=“用于以xml格式生成分类报告的Webservice”)]
公共字符串getCategoryWiseReport_2(字符串区)
{
字符串s=null;
C_代码的字符串=null;
字符串ofc_desg=null;
字符串of c_name=null;
字符串dep_name=null;
字符串总计_comp=null;
字符串挂起=空;
string desposed=null;
字符串中间值=null;
字符串默认值=null;
字符串arr[];
var con=new SqlConnection(“数据源=xxxx;初始目录=xyz;集成安全性=true”);
StringBuilder JSON=新的StringBuilder();
con.Open();
var cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText=“ReportSummery”;
cmd.CommandType=CommandType.storedProcess;
DataTable dt=新的DataTable();
SqlDataAdapter ad=新的SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue(“@district”,district);
cmd.ExecuteNonQuery();
ad.Fill(dt);
对于(int i=0;i
我想你这样做有点困难。首先,如果您不想在
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吗?
string json = JsonConvert.SerializeObject(data);