已有56人关注
《C#开发实战宝典》例14.3关于ExecuteScalar的问题
发表在C#图书答疑 2010-05-23
是否精华
版块置顶:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TM14._3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection conn;
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=");
            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);
            }
        }
    }
}

结果显示的总是1条数据而不是书上的多条,ExecuteScalar()不是返回第一行第一列的吗?我使新手,请多指教。
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2010-05-24
沙发
读者朋友:
    您好,显示1条数据说明你的数据库中有1条数据,你打开数据库看一下你要查看的表,把其中多添加几条数据,然后再执行这个程序,就会看到显示多条数据了。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照