2013-09-05 154 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

相關問題