如何在asp.net中的cmd.parameters.addwithvalue()中一次绑定多个值
我正在开发 ASP.NET 应用程序。我在 web.config 中的同一个键有 3 个值 例如:添加 key="ids" value="12333,43434343,434232"。
在 aspx.cs 中,我像这样调用。
string merchantIds = ConfigurationManager.AppSettings["ids"];
string paramName = null;
foreach (string ids in merchantIds.Split(','))
{
paramName = ids;
}
com.Parameters.AddWithValue("@FBUserID", paramName);
我只得到最后一个 id 输出。我想在这里显示与 3 个 id 相关的输出。
请帮助我解决这个问题。
提前致谢, 易卜拉欣。
I am developing asp.net application. I have 3 values for the same key in web.config
eg: add key="ids" value="12333,43434343,434232".
In aspx.cs iam calling like this.
string merchantIds = ConfigurationManager.AppSettings["ids"];
string paramName = null;
foreach (string ids in merchantIds.Split(','))
{
paramName = ids;
}
com.Parameters.AddWithValue("@FBUserID", paramName);
I am only getting the last id output. I want to display outputs related to 3 ids here.
Please help me regarding this..
Thanks in advance,
Ibrahim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 SQL Server 2008,则可以将多个值传递到存储过程以使用表值参数进行连接。
替代方案是按照 Matthew 建议的那样传入 CSV,或者提供 XML blob 值。我在这里比较了 3 种方法:http://www.adathedev.co.uk/2010/02/sql-server-2008-table-valued-parameters.html
在我的测试中,表值参数表现最好,其次是 XML 方法,然后是 CSV 方法。
If you're using SQL Server 2008, you can pass in multiple values to a sproc to join on using Table Valued Parameters.
The alternatives are to pass in CSV as Matthew suggested, or supply an XML blob of values in. I've compared the 3 approaches here: http://www.adathedev.co.uk/2010/02/sql-server-2008-table-valued-parameters.html
In my tests on there, Table Valued Parameters performed best, followed by the XML approach, and then the CSV approach.
参数是单一的,不能让一个参数呈现多个值。您需要修改您的过程以处理逗号分隔的ID并在过程中完成工作,或者对于每个ID,您为每个ID运行过程...
编辑最简单的方法正在这样做:
理想情况下,您应该考虑这样处理您的商家 ID 是否真的是最好的设计。
哦,如果你在已经给出的答案中找到了正确的答案,请确保你接受它,否则我们所有的 StackOverflowers 真的是在无偿地给你答案......
Parameters are singular, you can't have a parameter present multiple values. You need to either modify your proc to handle comma-delimited ids and do the work in the proc instead, or for each of the Ids, you run the procedure for each one...
EDIT The simplest way is doing this:
Ideally you should be considering whether handling your merchant Ids like this is really the best design.
Oh, and if you find the right answer with those that have been given, please make sure you accept it as such, otherwise all us StackOverflowers are really giving you answers for nothing....
您的 foreach 循环将所有值写入同一个变量,因此仅使用最后一个值。就像 Matthew 所说,你必须编辑 proc 来获取逗号分隔的字符串,或者运行 proc 3 次
Your foreach loop is writing the all the values to the same variable, so only the last one will be used. Like Matthew said, you have to edit the proc to take your comma delimited string, or run the proc 3 times