如何为参数化的sql查询创建循环 [英] How to create a loop for the parameterized sql query

查看:50
本文介绍了如何为参数化的sql查询创建循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为下面一段代码创建一个循环。

任何人都可以提供帮助吗?







  string  sql =   SELECT TILE =' + tile [ 0 ] 
+ UNION ALL SELECT tile =' + tile [ 1 ] + '
+ UNION ALL SELECT tile =' + tile [ 2 ] + '
+ UNION ALL SELECT tile =' + tile [ 3 ] + '



+ UNION ALL SELECT tile =' + tile [n-1] + < span class =code-string> '
+ UNION ALL SELECT tile =' + tile [n] + ';





使它变得像



 string sql1 =   SELECT TILE =' + tile [ 0 ] 
+ UNION ALL SELECT ' + tile [ 1 ] + '

for i = 1; i< 100; i ++)
{
string sql2 = UNION ALL SELECT tile = tile [i + 1];
string sql = sql1 + sql2;
}





如果按图所示进行循环,则只能打印最后一个图块(从文本框输入)。

任何人都可以提供帮助吗?

解决方案

嗯......那里有一些问题。

你注意到的那个很简单:

   i =  1 ; i< 100; i ++)
{
string sql2 = UNION ALL SELECT tile = tile [i + 1];
string sql = sql1 + sql2;
}

将始终存在相同的字符串,因为每次循环都会覆盖 sql 的先前内容sql2 而不是添加到它们。

可能,你想要的更接近:

  for  i =  1 ; i< 100; i ++)
{
string sql2 = UNION ALL SELECT tile = tile [i + 1];
string sql1 + = sql2;
}
sql + = sql1;

但这不会起作用,因为:

1)你错过了结束双引用!

2)你需要一个空间来分隔UNION部分:

  for  i =  1 ; i< 100; i ++)
{
string sql2 = UNION ALL SELECT tile = tile [i + 1];
string sql1 + = sql2;
}
sql + = sql1;



1)你需要一个不是字符串的数字:

  for  i =  1 ; i< 100; i ++)
{
string sql2 = string .Format( UNION ALL SELECT tile = tile [{0}],i + 1 );
string sql1 + = sql2;
}
sql + = sql1;





但是如果你打算这样做,我' d强烈建议你开始使用StringBuilder而不是字符串连接!


你不必创建更多变量,你可以简单地分配它像



  for  i =  1 ; i< ; 100; i ++)
{
sql1 + = UNION ALL SELECT tile =' + tile [i + 1] + ';;
}





确保为循环计数器使用正确的值。


可能更短更简单的版本可能像



  string  sql =   SELECT TILE =' +  string  .Join( 'UNION ALL SELECT TILE =',tile)


Hi, I would like to create a looping for the following piece of code.
Anyone can help?



string sql = "SELECT TILE = '" + tile[0]
+ " UNION ALL SELECT tile= '" + tile[1] + "'"
+ " UNION ALL SELECT tile= '" + tile[2] + "'"
+ " UNION ALL SELECT tile= '" + tile[3] + "'"
.
.
.
+ " UNION ALL SELECT tile= '" + tile[n-1] + "'"
+ " UNION ALL SELECT tile= '" + tile[n] + "'";



to make it become like

string sql1 = "SELECT TILE = '" + tile[0]
+ " UNION ALL SELECT '" + tile[1] + "'"

for i=1;i<100;i++)
{
string sql2 = "UNION ALL SELECT tile = tile[i+1];
string sql = sql1+sql2;
}



If i did the loop as shown, it is able to print only the last tile (input from textbox ).
Anyone can help on this?

解决方案

Um...There are a few things wrong there.
The one you have noticed is simple:

for i=1;i<100;i++)
{
string sql2 = "UNION ALL SELECT tile = tile[i+1];
string sql = sql1+sql2;
}

Will always exist with the same string, because each time round the loop you overwrite the previous content of sql and sql2 instead of adding to them.
Probably, what you want is closer to:

for i=1;i<100;i++)
{
string sql2 = "UNION ALL SELECT tile = tile[i+1];
string sql1 += sql2;
}
sql += sql1;

But that won't work either because:
1) You are missing the end double quote!
2) You need a space to separate the UNION parts:

for i=1;i<100;i++)
{
string sql2 = " UNION ALL SELECT tile = tile[i+1]";
string sql1 += sql2;
}
sql += sql1;


1) you need a number not a string:

for i=1;i<100;i++)
{
string sql2 = string.Format(" UNION ALL SELECT tile = tile[{0}]", i + 1);
string sql1 += sql2;
}
sql += sql1;



But if you are going to do things like that, I'd strongly suggest you start looking at using a StringBuilder instead of string concatenation!


You don't have to create more variables, you can simply assign it like

for i=1;i<100;i++)
{
    sql1 += "UNION ALL SELECT tile = '" + tile[i+1] + "';";
}



Make sure you are using proper value for the loop counter.


Probably a shorter and simple version could be like

string sql = "SELECT TILE = '" + string.Join("' UNION ALL SELECT TILE = '", tile)


这篇关于如何为参数化的sql查询创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆