请问使用SqlCommand 和 SqlDataReader 操作数据库,如何才能得到该SQL串所查到的总行数?以及如何让datareader向下一行?
string sSqlConn;
string Sql;
SqlCommand com;
SqlConnection conn;
SqlDataReader dataRader; sSqlConn = ConfigurationManager.ConnectionStrings["MainStr"].ConnectionString; conn = new SqlConnection();
conn.ConnectionString = sSqlConn; Sql = "Select Text,TitleUrl,ID from Blog_Content where PostType = 2";
com = new SqlCommand(Sql, conn);
conn.Open(); dataRader = com.ExecuteReader();
dataRader.Read();
string sSqlConn;
string Sql;
SqlCommand com;
SqlConnection conn;
SqlDataReader dataRader; sSqlConn = ConfigurationManager.ConnectionStrings["MainStr"].ConnectionString; conn = new SqlConnection();
conn.ConnectionString = sSqlConn; Sql = "Select Text,TitleUrl,ID from Blog_Content where PostType = 2";
com = new SqlCommand(Sql, conn);
conn.Open(); dataRader = com.ExecuteReader();
dataRader.Read();
{
dataRader["列名"]
}
明白它的机制你就不会有这样的问题了
s.RecordsAffected是该命令影响的总行数
向下就s.read();
并不包括查询把。
返回-1 表示 SELECT 语句
--------重新读一遍。DataReader 在某些场合不见得一定比 Fill DataTable 好,有时候甚至不适用
不要乱听评论,要根据需求来选择