c# - 错误 :Index (zero based) must be greater than or equal to zero and less than the size of the argument list

标签 c# asp.net gridview ado.net

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 条件的情况下尝试但它不起作用时,请帮助我

最佳答案

我认为这是

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-parameters prevent sql-injection .

关于c# - 错误 :Index (zero based) must be greater than or equal to zero and less than the size of the argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18639356/

相关文章:

android - 交换 GridView 中的元素

c# - 使用数学算法随着时间的推移增加值(value)

c# - 使用 .NET Core 在 Windows 上执行 cmd.exe 命令 (webpack) 并保留颜色和输出

c# - 使用 Restful asp.net Web api 和安全 API 进行用户登录身份验证

jquery - 如何在不使用runat服务器的情况下访问代码隐藏中的html控件值?

android - 在不同的 Activity 或 Fragment 之间共享数据的正确方法是什么?

c# - 切换到版本 6.8 后,EB GUIDE Studio 扩展不再工作

c# - 我如何使用 XDocument.Load() 获取应用程序目录中的文件

c# - 将循环语句转换为 linq 模式

ASP.NET 多个 Eval 字段