C# 用sql查询填充数组
是否仍然可以用sql查询填充多维数组?透视表上的我的sql查询结果。正如你们所看到的,我想对总计列和总计行进行一些求和,我想知道在该表中求和和和百分比的最佳方法。。我说多维数组是因为它可能更容易。下面的代码可以工作,但我想知道如何选择行和列来求和。我正在使用ASP.NETCC# 用sql查询填充数组,c#,sql,arrays,pivot-table,C#,Sql,Arrays,Pivot Table,是否仍然可以用sql查询填充多维数组?透视表上的我的sql查询结果。正如你们所看到的,我想对总计列和总计行进行一些求和,我想知道在该表中求和和和百分比的最佳方法。。我说多维数组是因为它可能更容易。下面的代码可以工作,但我想知道如何选择行和列来求和。我正在使用ASP.NETC protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=****;Initial Catalog=***;User=sa;password=***");
//query for all specialities
string specstring = "SELECT Speciality.Shortname, SUM(1) as contar " +
"FROM DoctorEnterpriseDetails INNER JOIN " +
"Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId INNER JOIN " +
" GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId " +
" WHERE (DoctorEnterpriseDetails.EnterpriseId = 48) " +
" GROUP BY Speciality.Shortname ";
SqlCommand command = new SqlCommand(specstring, conn);
command.Connection.Open();
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = command;
// Adds rows in the DataSet
//DataSet myDataSet = new DataSet();
DataTable specstringtable = new DataTable();
myDataAdapter.Fill(specstringtable);
specstring = "";
for (int i = 0; i < specstringtable.Rows.Count; i++)
{
if (specstring == "")
{
specstring = "[" + specstringtable.Rows[i][0] + "]".ToString();
}
else
{
specstring = specstring + ", " + "[" + specstringtable.Rows[i][0] + "]";
}
}
//GroupGrid.DataSource = cobtable;
//GroupGrid.DataBind();
command.Connection.Close();
////query for pivot table
string querystring = "SELECT Description AS Categoria, " + specstring +
"FROM (SELECT GroupType.Description, Speciality.Shortname, SUM(1) AS contar, GroupType.GroupId " +
"FROM DoctorEnterpriseDetails INNER JOIN " +
"Speciality ON DoctorEnterpriseDetails.Speciality1 = Speciality.SpecialityId INNER JOIN " +
"GroupType ON DoctorEnterpriseDetails.GroupId = GroupType.GroupId " +
"WHERE (DoctorEnterpriseDetails.EnterpriseId = 48) " +
"GROUP BY GroupType.Description, Speciality.Shortname, DoctorEnterpriseDetails.GroupId, GroupType.GroupId) as ps " +
"PIVOT (SUM(contar) FOR Shortname IN (" + specstring + ")) pvt " +
"ORDER BY GroupId; ";
////Response.Write(querystring);
SqlCommand command2 = new SqlCommand(querystring, conn);
command2.Connection.Open();
SqlDataAdapter myDataAdapter2 = new SqlDataAdapter();
myDataAdapter2.SelectCommand = command2;
// Adds rows in the DataSet
//DataSet myDataSet2 = new DataSet();
DataTable cobtable = new DataTable();
myDataAdapter2.Fill(cobtable);
DataColumn cl = cobtable.Columns.Add("Total");
cobtable.Columns["Total"].SetOrdinal(1);
DataRow dr;
dr = cobtable.NewRow();
dr["Categoria"] = "Total";
cobtable.Rows.InsertAt(dr, 0);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 1);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 3);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 4);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 6);
dr = cobtable.NewRow();
dr["Categoria"] = "";
cobtable.Rows.InsertAt(dr, 7);
dr = cobtable.NewRow();
dr["Categoria"] = "%";
cobtable.Rows.InsertAt(dr, 9);
GroupGrid.DataSource = cobtable;
GroupGrid.DataBind();
}
c到目前为止,我尝试使用sql datareader,我可以用表适配器填充数据表并将其绑定到gridview。。它是有效的。。。但在那之后,我如何选择索引行进行求和呢@Andykorneyev先看一眼。然后用相关代码更新你的问题,并添加更多关于你想做什么和出了什么问题的细节。它将帮助其他人清楚地理解您的问题并给出适当的答案。好的,现在我看到了很多代码;但我还是不明白——你到底有什么问题。您正在从数据库中填充一些数据表,然后在数据表的行上进行迭代—所以为什么不在这个迭代中计算您需要的所有总和呢?我遗漏了什么吗?有人对我说使用多维数组会更容易,这就是为什么我问@andykorneyvwell,我不认为使用多维数组而不是数据表可以简化你的任务。相反,它会降低代码的可读性,而不会带来任何显著的好处。这个概念仍然是一样的:您仍然需要对行和列进行迭代并计算总和——不管是dataRows还是在数组中进行索引计算。