如何在C#中将动态数组传递给Sql查询 [英] How Can I Pass Dynamic Array To Sql Query In C#

查看:148
本文介绍了如何在C#中将动态数组传递给Sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有一个数组,





  string  [] ClassEightstudents = ctx_School.Students.Where(x = >  x.StudentClass ==    8)。选择(x = >  x.StudentName)。ToArray( ); 





现在我想在查询中传递这个C#,



 cmd.CommandText = 选择 * 来自 CricketTable 其中​​ studentname   ' + ClassEightstudents +' 

解决方案

参考:参数化SQL IN子句 [ ^ ]



您可以参数化每个值,例如:



  string  [] tags =  new   string  [] {  ruby​​  rails   scruffy  ruby​​onrails}; 
string cmdText = 从CricketTable中选择*其中studentname IN({0});

string [] paramNames = tags.Select(
(s,i)= > @ tag + i.ToString()
) .ToArray();

string inClause = string .Join( ,paramNames);
使用(SqlCommand cmd = new SqlCommand( string .Format(cmdText,inClause))){
for int i = 0 ; i < paramNames.Length; i ++){
cmd.Parameters。 AddWithValue(paramNames [i],tags [i]);
}
}





哪位能给你:



 cmd.CommandText =  从CricketTable中选择*,其中studentname IN(@ tag0,@ tag1,@ tag2,@ tag3) 
cmd.Parameters [ @ tag0] = ruby​​
cmd.Parameters [ @ tag1] = rails
cmd.Parameters [ @ tag2] = scruffy
cmd.Parameters [ @ tag3] = ruby​​onrails


你好n使用



  string  [] ClassEightstudents = ctx_School.Students.Where (x = >  x.StudentClass ==   8 )。选择(x = >  x.StudentName)。ToArray(); 

string str = 从CricketTable中选择*,其中studentname为(' + String .Join( ',',ClassEightstudents)+ ');


我做了这个解决方案,并为我工作。

  string  [] ClassEightstudents = ctx_School.Students.Where(x = >  x.StudentClass ==   8)。选择(x = >  x。 StudentName).ToArray(); 

foreach var item in ClassEightstudents)
{
ClassEightstudentsformat = ClassEightstudentsformat + ' + item + ' + ;
}
ClassEightstudentsformat = ClassEightstudentsformat .TrimEnd(' ,');





然后将其传递给命令



 cmd.CommandText = 选择 * 来自 CricketTable 其中 studentname   ' + ClassEightstudentsformat +' 


For Example I have an array,


string[] ClassEightstudents= ctx_School.Students.Where(x => x.StudentClass== "8").Select(x => x.StudentName).ToArray();



Now I want to pass this in query C#,

cmd.CommandText=select * from CricketTable where studentname in '"+ClassEightstudents+"'

解决方案

Refer: Parameterize an SQL IN clause[^]

You can parameterize each value, so something like:

string[] tags = new string[] { "ruby", "rails", "scruffy", "rubyonrails" };
string cmdText = "select * from CricketTable where studentname IN ({0})";

string[] paramNames = tags.Select(
    (s, i) => "@tag" + i.ToString()
).ToArray();

string inClause = string.Join(",", paramNames);
using (SqlCommand cmd = new SqlCommand(string.Format(cmdText, inClause))) {
    for(int i = 0; i < paramNames.Length; i++) {
       cmd.Parameters.AddWithValue(paramNames[i], tags[i]);
    }
}



Which will give you:

cmd.CommandText = "select * from CricketTable where studentname IN (@tag0,@tag1,@tag2,@tag3)"
cmd.Parameters["@tag0"] = "ruby"
cmd.Parameters["@tag1"] = "rails"
cmd.Parameters["@tag2"] = "scruffy"
cmd.Parameters["@tag3"] = "rubyonrails"


You can use

string[] ClassEightstudents= ctx_School.Students.Where(x => x.StudentClass== "8").Select(x => x.StudentName).ToArray();

          string str = "select * from CricketTable where studentname in ('" + String.Join("','", ClassEightstudents) + "')";


i did this solution and its work for me.

string[] ClassEightstudents= ctx_School.Students.Where(x => x.StudentClass== "8").Select(x => x.StudentName).ToArray();

foreach (var item in ClassEightstudents)
  {
      ClassEightstudentsformat =ClassEightstudentsformat+ "'" + item + "'" + ",";
  }
   ClassEightstudentsformat =ClassEightstudentsformat .TrimEnd(',');



then pass it into command

cmd.CommandText=select * from CricketTable where studentname in '"+ClassEightstudentsformat +"'


这篇关于如何在C#中将动态数组传递给Sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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