gpt4 book ai didi

C# 'select count' sql 命令错误地从 sql server 返回零行

转载 作者:太空狗 更新时间:2023-10-29 21:06:34 25 4
gpt4 key购买 nike

我正在尝试从 SQL Server 表中返回行数。 “网络”上的多个来源显示以下是一种可行的方法,但它继续返回“0 行”。当我在管理工作室中使用该查询时,它工作正常并正确返回行数。我尝试过只使用简单的表名以及管理工作室喜欢的完全限定的表名。

            using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = sqlConnectionString;
cn.Open();

SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM [LBSExplorer].[dbo].[myTable]", cn);
countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count: " + countStart.ToString());
}

有什么可能导致它的建议吗?

最佳答案

我是这样写的:

using (SqlConnection cn = new SqlConnection(sqlConnectionString))
{
cn.Open();

using (SqlCommand commandRowCount
= new SqlCommand("SELECT COUNT(*) FROM [LBSExplorer].[dbo].[myTable]", cn))
{
commandRowCount.CommandType = CommandType.Text;
var countStart = (Int32)commandRowCount.ExecuteScalar();
Console.WriteLine("Starting row count: " + countStart.ToString());
}
}

关于C# 'select count' sql 命令错误地从 sql server 返回零行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6326208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com