SqlDataReader直接是不可以的, 要么用Sql语句,例如: string strQuery="select count(*) as datacount from tablename"; 然后用SqlDataReader访问datacount就行了;比较好的方法是用SqlDataAdapter+DataSet 那么用DataSet.Tables["tablename"].Rows.Count;
还有一种比较简单的方法,你先用 int aa=mycom.ExecuteNonQuery() 就行了,
String sql = "select count(*) as RecordNum from Orders "; OleDbCommand myCmd = new OleDbCommand(sql, myConnection); myCmd.Connection.Open(); int count=int.Parse(myCmd.ExecuteScalar().ToString()); myCmd.Connection.Close();
string strQuery="select count(*) as datacount from tablename"; int aa=mycom.ExecuteScalar(); 要加错误陷阱:tablename没有数据时,返回null,类型转换失败
要么用Sql语句,例如:
string strQuery="select count(*) as datacount from tablename";
然后用SqlDataReader访问datacount就行了;比较好的方法是用SqlDataAdapter+DataSet
那么用DataSet.Tables["tablename"].Rows.Count;
int aa=mycom.ExecuteNonQuery()
就行了,
OleDbCommand myCmd = new OleDbCommand(sql, myConnection);
myCmd.Connection.Open();
int count=int.Parse(myCmd.ExecuteScalar().ToString());
myCmd.Connection.Close();
int aa=mycom.ExecuteScalar();
要加错误陷阱:tablename没有数据时,返回null,类型转换失败