读取数据库数据:select命令中怎么设置参数?
string a= Request.QueryString[ "id1 "];//参数id1是上个页面传过来的数据库的序号
...
...
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter( "select * from data2 where 序号=???? ", objConnection1);
请问下我想把这条数据读取出来????处应该写什么?或者整个命令应该填什么啊?谢谢!!!
------解决方案--------------------OleDbDataAdapter objDataAdapter = new OleDbDataAdapter( "select * from data2 where 序号= ? ", objConnection1); // OleDb 中, ? (问号) 表示参数占位符,多个参数【按顺序】匹配!!!
objDataAdapter.SelectCommand.Parameters.Add( " ", a);
// ...
------解决方案--------------------OleDbDataAdapter objDataAdapter = new OleDbDataAdapter( "select * from data2 where 序号= "+a, objConnection1);
------解决方案--------------------用参数比较安全
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter( "select * from data2 where 序号=@p ", objConnection1);
再定义参数添加上就行了
或者
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter( "select * from data2 where 序号= ' "+a+ " ' ", objConnection1);
------解决方案--------------------string a= Request.QueryString[ "id1 "];//参数id1是上个页面传过来的数据库的序号
...
...
string strSql = string.format( "select * from data2 where 序号={0} ",a);
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSql , objConnection1);
------解决方案--------------------StringBuilder strSql = new StringBuilder();
strSql.Append( "select count(1) from data2 ");
strSql.Append( " where ID= @ID ");
SqlParameter[] cmdParms = {
new SqlParameter( "@ID ", SqlDbType.Int,4)
};
cmdParms[0].Value = ID;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds ");
cmd.Parameters.Clear();
}
catch (
System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
......
凑合着看吧.