码迷,mamicode.com
首页 > Windows程序 > 详细

C# ExecuteScalar用法

时间:2017-09-18 17:32:05      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:code   sql   component   except   new   exe   button   ini   bsp   

using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CommandThreeProperties
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_12;uid=sa;pwd=sa");
            conn.Open();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Open || textBox1.Text != "")
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select count(*) from " + textBox1.Text.Trim();
                    cmd.CommandType = CommandType.Text;
                    int i = Convert.ToInt32(cmd.ExecuteScalar());//返回查询结果的第一行第一列
                    label2.Text = "数据表中共有:" + i.ToString() + "条数据";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

 

C# ExecuteScalar用法

标签:code   sql   component   except   new   exe   button   ini   bsp   

原文地址:http://www.cnblogs.com/stringAdmin/p/7543583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!