日期:2014-05-18  浏览次数:20459 次

怎样将URL传得的值绑定到SQL语句?
问题:
我想让app=Table1,应该怎么实现那?
-----------------------------------------
我通过URL得到的值:
string   app=Request.QueryString[ "Type "];


我想让app代替或等于table1:
SqlDataAdapter   SqlDataAdapter1   =   new   SqlDataAdapter( "select   *   from   Table1 ",   SqlConnection1);
------------------------------------------

------解决方案--------------------
string app=Request.QueryString[ "Type "].tostring;

SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter( "select * from "+app+ " ", SqlConnection1);

or
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter( "select * from '+app+ ' ", SqlConnection1);
------解决方案--------------------
string app = Request.QueryString[ "Type "].ToString();

SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter( "select * from "+ app, SqlConnection1);

------解决方案--------------------
上面说的都可以,我也来说一个吧。用存储过程来实现:
create proc select_app
(
@app varchar(20)
)
as
begin transaction
declare @count int
select * from @app
select @count=@@error
if(@count=0)
commit transaction
else
rollback transaction

在C#中这样调用:
public DataSet selectByApp(string app)
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter( "select_app ", connectString);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.Add(new SqlParameter( "@app ",app));
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
catch(Exception Err)
{
return DataErr(Err);
}
}

public DataSet DataErr(Exception Err) //简单的错误处理,呵呵
{
DataSet ErrDataSet = new DataSet( "Errors ");
DataTable ErrDataTable = ErrDataSet.Tables.Add( "Error ");
ErrDataTable.Columns.Add( "Message ");
ErrDataTable.Rows.Add(new object[] { Err.Message });
return ErrDataSet;
}
------解决方案--------------------
就在cs里生成sql语句就行了.