VS2017查询SQL Server数据库表有多少数据

using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 酒店信息管理3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlConnection conn;


        private void button2_Click(object sender, EventArgs e)
        {
            string queryString = "select count(*)as stuNum from GuestInfo";
            string connectionString = "server = localhost; database = HotelManage; Integrated Security = True; ";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        int zh = 0;
                        zh = Convert.ToInt32(reader["stuNum"]); //强制数据转换
                        label4.Text = "数据表中共有:" + zh.ToString() + "条数据";
                    }
                }
                finally
                {
                    // Always call Close when done reading.
                    reader.Close();
                }
            }
        }


    }
}

猜你喜欢

转载自blog.csdn.net/qq_40307919/article/details/81053702