现在自定义一个方法,其主要功能是对数据库执行一个SQL查询,并将查询结果放到DataSet中返回,函数如下:// 执行一个sql[Select]语句将结果返回dataset
// tblName 是要填充到DataSet中的表名
public static DataSet ExecuteSqlForDataSet(string strSql,string tblName)
{
strSql = FilterSQL(strSql);
SqlConnection MyConn = new SqlConnection(GetSqlConnString());
try
{
MyConn.Open();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSql,MyConn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds,tblName);
MyAdapter.Dispose();
MyConn.Close();
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
MyConn.Close();
}
}
现在有如下三条SQL语句:
strSQL_A = "Select n_cataid as s_cata_title,LEFT(s_title,10) as s_title,t_content From News order by n_cataid Desc";strSQL_B = "Select cata.s_title as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News,cata where news.n_cataid=cata.id order by cata.id Desc";strSQL_C = "Select cata.s_title as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News inner join cata on news.n_cataid=cata.id order by cata.id Desc";将它分别放到SQL Server查询分析器里都可以正确执行,但我把它放到asp.net(c#)代码中,调用如上ExecuteSqlForDataSet()方法执行,只有strSQL_A这条语句能执行,其它两条都不可以.
出现这样一个错误提示:异常详细信息: System.Exception: 在关键字 'order' 附近有语法错误。我找了很多资料,都没有找到答案,还请高手指点!小弟谢了~!
// tblName 是要填充到DataSet中的表名
public static DataSet ExecuteSqlForDataSet(string strSql,string tblName)
{
strSql = FilterSQL(strSql);
SqlConnection MyConn = new SqlConnection(GetSqlConnString());
try
{
MyConn.Open();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSql,MyConn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds,tblName);
MyAdapter.Dispose();
MyConn.Close();
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
MyConn.Close();
}
}
现在有如下三条SQL语句:
strSQL_A = "Select n_cataid as s_cata_title,LEFT(s_title,10) as s_title,t_content From News order by n_cataid Desc";strSQL_B = "Select cata.s_title as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News,cata where news.n_cataid=cata.id order by cata.id Desc";strSQL_C = "Select cata.s_title as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News inner join cata on news.n_cataid=cata.id order by cata.id Desc";将它分别放到SQL Server查询分析器里都可以正确执行,但我把它放到asp.net(c#)代码中,调用如上ExecuteSqlForDataSet()方法执行,只有strSQL_A这条语句能执行,其它两条都不可以.
出现这样一个错误提示:异常详细信息: System.Exception: 在关键字 'order' 附近有语法错误。我找了很多资料,都没有找到答案,还请高手指点!小弟谢了~!
解决方案 »
- C#泛型两个集合对比?
- ▲▲▲▲c# 操作 word 如何定位到表格中的某一单元格
- winform传值问题
- DataGrid有个问题,有关CurrentCellChanged事件
- 有三个类Class1,Class2,Class3...
- 关于采用生成本地化代码方法的多国语言版本的问题
- c#如何记录用户的操作日志(想通过xml来完成)
- HTTPWEBREQUEST 操作超时很费解
- 请问如何遍历窗体的控件,并判断是什么控件!
- 关于字符串的一个问题!高手请进,UP者有分!!
- 初来,献上一个免费的大学视频教学资源下载的网站
- ####求<<精通C#与ASP.NET程序设计 >>一书的光盘源代码!!!附电子版下载地址,超级好!纯C#为基础的ASP.NET教程!!!
{
MyConn.Close();
}
这句并不合理,最好是判断一下,MyConn是否打开的,否则将可以抛出另外的异常。
对于SQL不能查询的原因,你自己在企业管理器中试试,成功的情况下使用你自己定义的函数。
以上三条SQL语句,若将SQL_B和SQL_C中条件比较换成大于(>)或小于(<)的话,在查询分析器里和函数里都可以执行,换成等于(=)就出现楼主所说情况。
n_cataid as s_cata_title,
strSQL_B = "Select cata.id as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News,cata where news.n_cataid=cata.id order by cata.id Desc";strSQL_C = "Select cata.id as s_cata_title,LEFT(News.s_title,10) as s_title,news.t_content From News inner join cata on news.n_cataid=cata.id order by cata.id Desc";