当前位置: 代码迷 >> ASP.NET >> asp.net数据库结果显示有关问题~
  详细解决方案

asp.net数据库结果显示有关问题~

热度:2969   发布时间:2013-02-25 00:00:00.0
asp.net数据库结果显示问题~~~
public int tieziCount(){

conn = new SqlConnection("server=.;database=BBS;uid=sa;pwd=");
conn.Open();

string sql3 = "select count(*) from fabiaoneirong";

da = new SqlDataAdapter(sql3,conn);
da.Fill(ds,"aa");
int a = 
int.Parse(ds.Tables["aa"].Rows[0][0].ToString());
return a;
}

我想将表的行的总数传出去,但是怎么做啊??
int a = int.Parse(ds.Tables["aa"].Rows[0][0].ToString());这里出错了。

给个详细代码好么??谢谢~~~~~~

------解决方案--------------------------------------------------------
用这个函数就行了,建议用ExecuteScalar单行单列数据。
C# code
         /// <summary>    /// 返回一个数据    /// </summary>    /// <param name="strSql"></param>    /// <returns></returns>    public string ExecuteScalar(string strSql)    {        string tmp = "";        SqlConnection conn = new SqlConnection(ConnectionString);        try        {            conn.Open();            SqlCommand cmd = new SqlCommand(strSql, conn);            tmp = cmd.ExecuteScalar().ToString();            conn.Close();        }        catch { conn.Close(); }        finally        {            conn.Close();            conn.Dispose();//释放连接        }        return tmp;    }         //调用        string sql3 = "select count(*) from fabiaoneirong";         int count=Convert.Int32(ExecuteScalar(sql3));//行总数
------解决方案--------------------------------------------------------
int count=Convert.ToInt32(ExecuteScalar(sql3));//行总数
也可用int.Parse转换成整型,需注意下异常处理。
------解决方案--------------------------------------------------------
ds.Tables["aa"].Rows.Count
  相关解决方案