当前位置: 首页 > 编程语言 > C#

c#中如何计算sql表的行数?分享

时间:2023-04-10 12:23:25 C#

c#中如何统计sql表的行数?C#中如何统计sql表的行数?我需要从我的数据库中提取一些数据......你可以试试这个:selectcount(*)fromtablenamewherecolumnname='values'C#codewillbelikethis:-publicintA(){stringstmt="SELECTCOUNT(*)来自dbo.表名”;整数计数=0;using(SqlConnectionthisConnection=newSqlConnection("DataSource=DATASOURCE")){using(SqlCommandcmdCount=newSqlCommand(stmt,thisConnection)){thisConnection.Open();}count=(int)cmdCount.ExecuteScalar();}}返回计数;您需要先从C#建立数据库连接。然后您需要将以下查询作为commandText传递。Selectcount(*)fromTableName使用ExecuteScalar/ExecuteReader获取返回的计数。你是这个意思吗?SELECTCOUNT(*)FROMyourTableWHERE....你可以创建一个可以一直使用的全局函数以上是C#学习教程:如何在c#中统计sql表的行数?所有分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——publicstaticintGetTableCount(stringtablename,stringconnStr=null){stringstmt=string.Format("SELECTCOUNT(*)FROM{0}",表名);如果(String.IsNullOrEmpty(connStr))connStr=ConnectionString;整数计数=0;尝试{使用(SqlConnectionthisConnection=newSqlConnection(connStr)){使用(SqlCommandcmdCount=newSqlCommand(stmt,thisConnection)){thisConnection.Open();count=(int)cmdCount.ExecuteScalar();}}返回计数;}catch(Exceptionex){VDBLogger.LogError(ex);返回0;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: