如何存储数组中数据表的数据

时间:2014-09-02 07:27:51

标签: 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;
            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();
}

1 个答案:

答案 0 :(得分:0)

我认为你很难做到这一点。首先,如果您不希望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;}
}

然后填充来自sproc的数据 - 像“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);