程序如下:
目的是在Label1中显示该表的行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=yh-20120602uzvz\\SQLNEW;Initial Catalog=guestbook;Integrated Security=True"); SqlCommand comm = new SqlCommand("select count(*)from GuestBook", con);
con.Open();
int count = (Int32)comm.ExecuteScalar();
Label1.Text = count.ToString();
con.Close();
}
}
调试后报错:“int count = (Int32)comm.ExecuteScalar();附近有语法错误”(调试前无错误显示)
目的是在Label1中显示该表的行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=yh-20120602uzvz\\SQLNEW;Initial Catalog=guestbook;Integrated Security=True"); SqlCommand comm = new SqlCommand("select count(*)from GuestBook", con);
con.Open();
int count = (Int32)comm.ExecuteScalar();
Label1.Text = count.ToString();
con.Close();
}
}
调试后报错:“int count = (Int32)comm.ExecuteScalar();附近有语法错误”(调试前无错误显示)
object obj = comm.ExecuteScalar(); //返回类型为object if (obj != System.DBNull.Value)
{
count= (Int32)comm.ExecuteScalar();
}
object obj = comm.ExecuteScalar(); if (obj != null)
count = Convert.ToInt32(comm.ExecuteScalar());
object obj = comm.ExecuteScalar(); if (obj != null)
count = Convert.ToInt32(obj);
object count1 = comm.ExecuteScalar();
string count0 = count1.ToString();
int count = int.Parse(count0);
后类型转换没错了,但、、、错误还是跟原来一样,求大神解救啊!
qlCommand comm = new SqlCommand("select count(*)from GuestBook", con);中的sql语句,count(*)这部分的左右括号不匹配,可能一个是中文的括号。