2013-09-05 156 views
3
SqlConnection cn = new SqlConnection("server=localhost;initial catalog=newmits;trusted_connection=true"); 
cn.Open(); 
string a = string.Format("select * from upnotice where show like '{0}' ,%t"); 
SqlDataAdapter adp1 = new SqlDataAdapter(a, cn); 
DataSet ds1 = new DataSet(); 
adp1.Fill(ds1); 
GridView1.DataSource = ds1; 
GridView1.DataBind(); 

当我尝试没有where条件它的工作原理,但与它不工作请帮助我错误:索引(从零开始)必须大于或等于零且小于参数列表的大小

回答

7

我认为这

string a = string.Format("select * from upnotice where show like '{0}' ,%t"); 

应该

string a = string.Format("select * from upnotice where show like '{0}'","%t"); 

每个格式项(如{0}{1})需要有相应的参数。

但是,您不应该使用string.Format,而是将sql参数设置为prevent sql-injection

相关问题