程序运行时没有错误,但没有给出所需的输出
本文关键字:输出 运行时 有错误 程序 | 更新日期: 2023-09-27 18:27:19
以下程序从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();
}
}
}
}
}
}
}
}
}
由于程序并没有抛出任何语法错误,我想这可能是一个逻辑错误。有人能帮我注意一下吗?提前谢谢。
系统工作正常,因为如果编写dt1.Rows[0].ToString(),则无法获得单元格的值。这是因为System.Data.DataRowSystem.Data.DDataRowSystem.Data.DataRowSystem.Data_DataRowSystem没有重写方法ToString()。
我认为你必须使用dt1.Rows[0].ItemArray[3]
或dt1.Rows[0]["column name"].ToString();
希望这能有所帮助。