日期:2014-05-19  浏览次数:21014 次

为什么带参数的SQL语句这样写就错误啊?
StringBuilder   strSql   =   new   StringBuilder();
strSql.Append( "select   NodeID,Text,ParentID,Location,OrderID,comment,Url,PermissionID,ImageUrl   from   S_Tree   ");
strSql.Append( "   where   NodeID=@NodeID ");
  Database   db   =   DatabaseFactory.CreateDatabase();
DbCommand   dbCommand   =   db.GetSqlStringCommand(strSql.ToString());
db.AddInParameter(dbCommand,   "NodeID ",   DbType.Int32,   NodeID);
SysNode   node   =   new   SysNode();
using   (IDataReader   dataReader   =   db.ExecuteReader(dbCommand))
{

}

修改1:

StringBuilder   strSql   =   new   StringBuilder();
strSql.Append( "select   *   from   S_Tree   ");
strSql.Append( "   where   NodeID=@NodeID ");
Database   db   =   DatabaseFactory.CreateDatabase();
DbCommand   dbCommand   =   db.GetSqlStringCommand(strSql.ToString());
db.AddInParameter(dbCommand,   "NodeID ",   DbType.Int32,   NodeID);
SysNode   node   =   new   SysNode();
using   (IDataReader   dataReader   =   db.ExecuteReader(dbCommand))
{

}

修改2:
StringBuilder   strSql   =   new   StringBuilder();
strSql.Append( "select   Text,ParentID,Location,OrderID,comment,Url,PermissionID,ImageUrl   from   S_Tree   ");
                        strSql.Append( "   where   NodeID=@NodeID ");
                        Database   db   =   DatabaseFactory.CreateDatabase();
                        DbCommand   dbCommand   =   db.GetSqlStringCommand(strSql.ToString());
                        db.AddInParameter(dbCommand,   "NodeID ",   DbType.Int32,   NodeID);
SysNode   node   =   new   SysNode();
using   (IDataReader   dataReader   =   db.ExecuteReader(dbCommand))
{

}

修改的1,2。都能运行,但为什么以前的就出错误啊。但   NodeID     和@NodeID   一个是字段,一个是变量啊。但现在怎么都认为是变量了?

------解决方案--------------------
db.AddInParameter(dbCommand, "NodeID ", DbType.Int32, NodeID);//...........