Gli*_*kot 6 .net c# sql-server
我正在尝试从SQL Server表返回rowcount.'net上的多个来源显示下面是一个可行的方法,但它继续返回'0行'.当我在管理工作室中使用该查询时,它工作正常并正确返回rowcount.我只是用简单的表名以及管理工作室倾向于喜欢的完全合格的名称来尝试它.
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());
}
Run Code Online (Sandbox Code Playgroud)
有什么可能导致它的建议吗?
这是我写的方式:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
将 CommandType 设置为 Text
command.CommandType = CommandType.Text
Run Code Online (Sandbox Code Playgroud)
来自Damien_The_Unknowner评论的更多详细信息,有关 .NET 是否默认 SqlCommandTypes 类型为文本。
如果你拆开 SqlCommand 上 CommandType 的 getter,你会发现有奇怪的特殊大小写,如果值当前为 0,它就会撒谎并说它是 Text/1(同样,从组件/设计的角度来看) ,默认值列为 1)。但实际的内部值保留为 0。
| 归档时间: |
|
| 查看次数: |
11890 次 |
| 最近记录: |