asp.net统计数据库表中某一列符合要求的个数,代码已写出,可以运行,但显示数量是-1 附图
怎么才能显示正确的符合要求的数量,求解惑usingSystem;usingSystem.Collections;usingSystem.Configuration;usi...
怎么才能显示正确的符合要求的数量,求解惑
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class tongji : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
String strconn = "Data Source=localhost;Initial Catalog=No.Se7enHotel;User ID=sa;password=123456";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string word1 = "空";
string word2 = "订";
string sele1 = "select count(RoomId) from roomStatus where Status='"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status='" + word2 + "'";
SqlCommand cmd1 = new SqlCommand(sele1,conn);
Label1.Text = (cmd1.ExecuteNonQuery()).ToString();
SqlCommand cmd2 = new SqlCommand(sele2,conn);
Label2.Text = (cmd2.ExecuteNonQuery()).ToString();
conn.Close();
}
} 展开
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class tongji : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
String strconn = "Data Source=localhost;Initial Catalog=No.Se7enHotel;User ID=sa;password=123456";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string word1 = "空";
string word2 = "订";
string sele1 = "select count(RoomId) from roomStatus where Status='"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status='" + word2 + "'";
SqlCommand cmd1 = new SqlCommand(sele1,conn);
Label1.Text = (cmd1.ExecuteNonQuery()).ToString();
SqlCommand cmd2 = new SqlCommand(sele2,conn);
Label2.Text = (cmd2.ExecuteNonQuery()).ToString();
conn.Close();
}
} 展开
3个回答
展开全部
两种办法:
1、在中文字符串前加N,如下:
string sele1 = "select count(RoomId) from roomStatus where Status=N'"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status=N'" + word2 + "'";
2、换用like
string sele1 = "select count(RoomId) from roomStatus where Status like '%"+word1+"%'";
string sele2 = "select count(RoomId) from roomStatus where Status like '%" + word2 + "%'";
1、在中文字符串前加N,如下:
string sele1 = "select count(RoomId) from roomStatus where Status=N'"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status=N'" + word2 + "'";
2、换用like
string sele1 = "select count(RoomId) from roomStatus where Status like '%"+word1+"%'";
string sele2 = "select count(RoomId) from roomStatus where Status like '%" + word2 + "%'";
更多追问追答
追问
我都试了,都不行, —_—~!
追答
那在
string sele1 = "select count(RoomId) from roomStatus where Status='"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status='" + word2 + "'";
下面加两句:
Response.Write(sele1);
Response.Write("");
Response.Write(sele2);
然后,执行时,在浏览器上面把页面打印出来的这两句SQL语句,在数据库中执行一次,最好发上来给大伙看看。
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
导致这个问题的原因很多
一个简单的方法
string sele1 = "select count(RoomId) from roomStatus where Status='"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status='" + word2 + "'";
这两句加断点,调试,把这两句 复制出来在数据库里面执行以下 看看能不能得到你想要的答案
估计是你sql语句本身的原因
或者是数据库字符集、或者该字段类型、或者文件后面有空格之类
一个简单的方法
string sele1 = "select count(RoomId) from roomStatus where Status='"+word1+"'";
string sele2 = "select count(RoomId) from roomStatus where Status='" + word2 + "'";
这两句加断点,调试,把这两句 复制出来在数据库里面执行以下 看看能不能得到你想要的答案
估计是你sql语句本身的原因
或者是数据库字符集、或者该字段类型、或者文件后面有空格之类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么是 -1,你首先要搞明白。
因为你传入的值是“中文”,但是,你的数据库并不是标准中文数据库,那么,在连接字符串上,想传入中文的话,就要传入UTF-8编码的中文。
所以说,对中文转码一次,应该就能够解决你的问题了。
因为你传入的值是“中文”,但是,你的数据库并不是标准中文数据库,那么,在连接字符串上,想传入中文的话,就要传入UTF-8编码的中文。
所以说,对中文转码一次,应该就能够解决你的问题了。
追问
具体怎么做呢,
我明白你转码的意思,但是我修改表中中文内容时都没出现问题,比如把状态“空”改为“订”之类的,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询