【问题标题】:Program runs without error but not giving desired output程序运行没有错误,但没有给出所需的输出
【发布时间】:2014-10-22 04:13:43
【问题描述】:

以下程序从 SQL Server 2008 表中提取数据,应用简单的 for 循环并计算记录总数。程序编译并成功运行,没有任何错误,但不会将记录总数打印到屏幕上。它不打印任何东西。 .cs(后面的代码)是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
namespace CountDocs
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnCount_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
           {
                using (SqlCommand cmd = new SqlCommand())
                {
                    String sql = "select * from dbo.Company";
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    con1.Open();
                    Int32 Total = 0;
                            Total = (Int32)cmd1.ExecuteScalar();
                            Console.WriteLine(Total);
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                    for (int i = 0; i < dt.Rows.Count; ++i)
                    {
                        string companyname;
                        companyname = dt.Rows[i].ItemArray[0].ToString();
                        SqlConnection con1 = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
                        {
                            using (SqlCommand cmd1 = new SqlCommand())
                            {
                                String sql1 = "select Count(*) from dbo.Documents where Src=" + "'" + companyname + "'";
                                cmd1.Connection = con1;
                                cmd1.CommandText = sql1;
                                con.Open();
                                DataTable dt1 = new DataTable();
                                Int32 Total = 0;
                                Total = (Int32)cmd1.ExecuteScalar();
                                Console.WriteLine(Total);
                                if (con.State == ConnectionState.Open)
                                {
                                    con.Close();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

由于程序没有抛出任何语法错误,我猜这可能是一个逻辑错误。有人可以帮我注意吗?提前致谢。

【问题讨论】:

  • 使用 cmd1.ExecuteScalar。没有必要使用 SqlDataAdapter。 ExecuteScalar - 执行查询,并返回查询返回的结果集中第一行的第一列。
  • @Seminda 你认为 SqlDataAdapter 会导致问题吗?
  • 没有。但如果您只需要一列,那么 ExecuteScalar 就在那里。它将使您的代码更简单,您可以很容易地识别您的问题。

标签: c# sql-server-2008-express


【解决方案1】:

系统工作正常,因为如果你写 dt1.Rows[0].ToString() 你没有得到单元格的值。那是因为 System.Data.DataRowSystem.Data.DataRowSystem.Data.DataRowSystem.Data.DataRowSystem 没有覆盖方法 ToString()

我认为你必须使用dt1.Rows[0].ItemArray[3]dt1.Rows[0]["column name"].ToString();

希望这会有所帮助。

【讨论】:

  • 我的眼睛刚刚发现了一个错误。 for 循环内的第二个连接 (con1) 必须打开和关闭,而不是 for 循环外的第一个连接 (con)。这样做了,现在系统运行成功,没有产生任何输出。
  • 什么不起作用?你有更多的信息吗?没有 cmets,您的代码很难阅读...
  • dt1.Rows[0].ItemArray[3].ToString() 代码抛出错误:Index was outside the bounds of the array. dt1 没有列名,所以不知道我应该如何使用您的第二个建议 (dt1.Rows[0]["column name"].ToString)。谢谢
  • 刚刚完成更新和上传代码。请查阅。谢谢
  • 当然你会得到一个 Index was outsite of the bounds of the array 异常,因为你必须用你需要的值替换 0 和 3...这只是一个例子...跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
相关资源
最近更新 更多