首页上一页 1 下一页尾页 1 条记录 1/1页
《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()不是返回第一行第一列的吗?我使新手,请多指教。
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()不是返回第一行第一列的吗?我使新手,请多指教。