为什么即使列名可用也出现无效列名的错误 [英] why i m getting error of invalid column name even when column name is available
本文介绍了为什么即使列名可用也出现无效列名的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string checkl = "SELECT COUNT(*) FROM Counter WHERE leftcount=1";
com = new SqlCommand(checkl, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter(com);
DataTable dtcheckl = new DataTable();
da.Fill(dtcheckl);
int lcount = 0;
lcount =Convert.ToInt32(dtcheckl.Rows[0]["leftcount"].ToString());//ERROR
我收到无效的列名错误,但是当我在数据库中运行相同的查询时,我得到了输出,但是在前端,我得到错误了,为什么?
i am getting invalid column name error but when i run same query in database i get the output but here in front end i m getting error why?
推荐答案
,因为您要引用.NET中未在SQL中检索的列.
从计数器WHERE leftcount = 1中选择COUNT(*).您正在选择COUNT(*)\
您应该选择在代码中使用的leftcount.
Because you are referring column in .NET that is not retrived in SQL.
SELECT COUNT(*) FROM Counter WHERE leftcount=1. You are selecting COUNT(*)\
you should be selecting leftcount for using it in Code .
在select语句中,您得到的是计数而不是列数据.您可以将查询设置为
In the select statement you are getting the count not the column data . you can set the query as
SELECT COUNT (*) as leftcount FROM Counter WHERE leftcount=1
希望您在修改查询后能得到理想的结果.
Hope you''ll get the desired result after modifying your query.
编写如下代码.
Write your code as below.
string checkl = "SELECT COUNT(*) as CountValue FROM Counter WHERE leftcount=1";
com = new SqlCommand(checkl, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter(com);
DataTable dtcheckl = new DataTable();
da.Fill(dtcheckl);
int lcount = 0;
lcount =Convert.ToInt32(dtcheckl.Rows[0]["CountValue"].ToString());
这应该可行.
This should work.
这篇关于为什么即使列名可用也出现无效列名的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文