access数据库的select...where条件问题.
string newsString = Request.QueryString[ "NewID "].ToString();
int newID=int.Parse(newsString.ToString());
int languageID = 0;
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[ "OTDB "].ToString();
OleDbConnection con = new OleDbConnection(connStr);
string sqlStr = "select NTitle,NContent From dbo_Fc_CompanyNews ";
sqlStr = sqlStr + newID;
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
this.Label1.Text =dr[ "NContent "].ToString();
this.LblNewsTitle.Text=dr[ "NTitle "].ToString();
//////////////////////
为什么这样写select 后面的where条件不起作用?
我直接 select NTitle,NContent From dbo_Fc_CompanyNews where new_class_id= ' "+NewID+ " ';这样写运行时出错.
------解决方案--------------------发ID为字符型,
string sqlStr = "select NTitle,NContent From dbo_Fc_CompanyNews where new_class_id= ' ";
sqlStr = sqlStr + newID+ " ' ";
------解决方案----------------------如果NID是字符型,按如下处理:
string sqlStr = "select NTitle,NContent From dbo_Fc_CompanyNews where NID= ' ";
sqlStr = sqlStr + newID+ " ' ";
--如果NID是数值型,按如下处理:
string sqlStr = "select NTitle,NContent From dbo_Fc_CompanyNews where NID= ";
sqlStr = sqlStr + newID;
如果还有问题,中断代码,把调试出的SQL语句直接在Access的查询中执行,看看是否正确。
------解决方案--------------------先要将NEWID转换为字符型,再组成SQL语句。