ASP.NET中的列名无效

LXPXHK 2009-08-06 01:59:36
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ck_score7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ck_Click(object sender, EventArgs e)
{
string xh = xuehao.Text.ToString();
string xqi = xq.Text.ToString();
string xni = xn.Text.ToString();
string kmu = km.Text.ToString();
SqlConnection conn = new SqlConnection("server=localhost;database=gydianzi;uid=sa;pwd=123");
conn.Open();
SqlCommand comm = new SqlCommand("select * from scorelist where s_name=xh and s_year=xni and s_term=xqi and s_sort=kmu", conn);
SqlDataReader dr = comm.ExecuteReader();
dr.Read();
xm.Text = dr["s_truename"].ToString();
yjzz.Text = dr["s_score1"].ToString();
cxsj.Text = dr["s_score2"].ToString();
ys.Text = dr["s_score3"].ToString();
zp.Text = dr["s_allscore"].ToString();
xf.Text = dr["s_credithour"].ToString();
}
}

问题:
“/CheckScore”应用程序中的服务器错误。
--------------------------------------------------------------------------------

列名 'xh' 无效。
列名 'xni' 无效。
列名 'xqi' 无效。
列名 'kmu' 无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: 列名 'xh' 无效。
列名 'xni' 无效。
列名 'xqi' 无效。
列名 'kmu' 无效。

源错误:


行 26: conn.Open();
行 27: SqlCommand comm = new SqlCommand("select * from scorelist where s_name=xh and s_year=xni and s_term=xqi and s_sort=kmu", conn);
行 28: SqlDataReader dr = comm.ExecuteReader();
行 29: dr.Read();
行 30: xm.Text = dr["s_truename"].ToString();

源文件: f:\Firstproject\CheckScore\ck_score2.aspx.cs 行: 28


...全文
985 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang17500332 2009-08-06
  • 打赏
  • 举报
回复
("select * from scorelist where s_name='"+xh+"' and s_year=’“+xni+”‘ and s_term='"+xqi+"' and s_sort='"+kmu+", conn);
应该可以了
s32702 2009-08-06
  • 打赏
  • 举报
回复
建议楼主使用SqlParameter,在数据库中进行拼装SQL 语句。这样能减少错误
s32702 2009-08-06
  • 打赏
  • 举报
回复
select * from scorelist where s_name="+ xh +" and s_year= " + xni +" and s_term=' " + xqi +" 'and s_sort="+ kmu +"
Lovely_baby 2009-08-06
  • 打赏
  • 举报
回复
你跟踪调试一下就知道了!
夏脑 2009-08-06
  • 打赏
  • 举报
回复
SqlCommand comm = new SqlCommand("select * from scorelist where s_name='"+xh+"' and s_year='"+xni+"' and s_term='"+xqi+"' and s_sort='"+kmu+"'", conn);
当然如果是数字字段就取消单引号
ivws_19 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bernardsun 的回复:]
这些都是参数啊要s_name='"+xh+"' and s_year='"+xni+"' and s_term='"+xqi+"' and s_sort='"+kmu+"'"
这么写,如果数据库字段是数字的就不要单引号
[/Quote]
up,加上隐号
rocker0325 2009-08-06
  • 打赏
  • 举报
回复
 SqlCommand comm = new SqlCommand("select * from scorelist where s_name='"+xh+"' and s_year='"+xni+"' and s_term='"+xqi+"' and s_sort='"+kmu+"'", conn); 
xml120 2009-08-06
  • 打赏
  • 举报
回复
SqlCommand comm = new SqlCommand("select * from scorelist where s_name=xh and s_year=xni and s_term=xqi and s_sort=kmu", conn);

SQL写的有问题。非INT型的列在sql中传值的时候要加 ' 号。

不然就不会被识别为字段值,而被误认为是字段名(即列)勒。
play1982 2009-08-06
  • 打赏
  • 举报
回复
SQL写的有问题

select * from xxx where xxx='xxx'
zdl110110 2009-08-06
  • 打赏
  • 举报
回复
SqlCommand comm = new SqlCommand("select * from scorelist where s_name=xh and s_year=xni and s_term=xqi and s_sort=kmu", conn); 这里不对,建议使用参数
nsl204 2009-08-06
  • 打赏
  • 举报
回复
什么列名啊,根本就是sql语句写得有问题啊
akt87 2009-08-06
  • 打赏
  • 举报
回复
这个很糟糕,LZ是否看下书上简单的例子,可能毛病有时就是LZ你的一点点小疏忽
xray2005 2009-08-06
  • 打赏
  • 举报
回复


SqlCommand comm = new SqlCommand("select * from scorelist where s_name='xh' and s_year='xni' and s_term='xqi' and s_sort='kmu'", conn
BernardSun 2009-08-06
  • 打赏
  • 举报
回复
这些都是参数啊要s_name='"+xh+"' and s_year='"+xni+"' and s_term='"+xqi+"' and s_sort='"+kmu+"'"
这么写,如果数据库字段是数字的就不要单引号
andrewsway 2009-08-06
  • 打赏
  • 举报
回复
说明绑到控件上的列名,和你的数据关联不对,有可能是数据库的名字,当然,也有可能是你的集合的名字,具体看你的数据处理方式了
  • 打赏
  • 举报
回复
列名 'xh' 无效。
列名 'xni' 无效。
列名 'xqi' 无效。
列名 'kmu' 无效。
数据库中有xh、xni、kmu列名么?
PandaIT 2009-08-06
  • 打赏
  • 举报
回复
while(dr.Read())
{
...code
}

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧