将多个查询的结果添加到集合并在div中显示集合

时间:2013-07-17 18:23:43

标签: c# asp.net-mvc

我希望能够运行下面的查询并在div中逐行返回结果。 结果被添加到哈希表中,我将传递给另一个方法并将其显示在div中。

数组arrayOfQueries [0]中的第一个查询可以返回多行 阙:

  1. 如何编写此代码来处理此数组中查询返回的多行--- arrayOfQueries [0]。即我希望能够在苛刻的表中存储多行,或者我应该使用stringbuilder对象。

  2. 如何在div中显示哈希表的内容。

    public Hashtable DisplayResultsToUsers(int genJobId)
    {
        Hashtable myHt = new Hashtable();
        try
        {
            string[] arrayOfQueries = new string[4];
            arrayOfQueries[0] = "select generic, customernumber, productcode from u_generic_data where row_status = 2 and generic_job_id =" + genJobId;
            arrayOfQueries[1] = "select count(*) from u_generic_data where row_status = 2 and generic_job_id =" + genJobId;
            arrayOfQueries[2] = "select count(*) from u_generic_data where row_status = 1 and generic_job_id =" + genJobId;
            arrayOfQueries[3] = "select count(*) from u_generic_data where row_status = 0 and generic_job_id =" + genJobId;
    
            string connect = ConfigurationManager.ConnectionStrings["myconnect"].ConnectionString;
    
            using (OdbcConnection resConnect = new OdbcConnection(connect))
            {
                string sqlStmt = string.Empty;
                string queryResult = string.Empty;
                int count = 0;
                OdbcCommand dbCmd = new OdbcCommand();
                dbCmd.Connection = resConnect;
                resConnect.Open();
    
                foreach (string s in arrayOfQueries)
                {
                    dbCmd.CommandText = s;
                    queryResult =  Convert.ToString(dbCmd.ExecuteScalar());
                    count = count + 1;
                    myHt.Add(count, queryResult);
                }
    
                resConnect.Close();
            }
    
        }
        catch (Exception ex)
        {
    
    
        }
    
        return myHt;
    

1 个答案:

答案 0 :(得分:0)

dbCmd.ExecuteScalar()无法使用select generic, customernumber, productcode from u_generic_data where row_status = 2 and generic_job_id =" + genJobId之类的查询。您需要执行dbCmd.ExecuteReader()或其他一些重载来检索这些列。对于ExecuteScalar()查询,select count(*)会很好。