我无法计算数据库表中的行数。 [英] I am unable to count rows from my database table.

查看:86
本文介绍了我无法计算数据库表中的行数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                cmd.CommandText = "select count(*) from test;";
                cmd.Connection = con;
                int a = (int)cmd.ExecuteScalar();
                dr.Close();
                con.Close();
                string status = Convert.ToString(a);
                label1.Text = status;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                this.Close();
            }
        }

推荐答案

我想你错过了连接细节



I think you miss the connection details

string connectionString = "Connection String details";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();


更好地了解这个

Better you follw this
//Connecion string .....
            SqlCommand cmd= new SqlCommand("select count(*) from test", con);
	    int noRows;
            con.Open();
	    noRows = cmd.ExecuteNonQuery();
	    con.Close();
	    label1.Text= Convert.ToString(noRows);






您没有发起连接或命令对象。



谢谢......
Hi,

You did not initiate your connection or command object.

Thanks...


这篇关于我无法计算数据库表中的行数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆