简体   繁体   English

C#:通过2d对象数组将参数传递给函数

[英]C# : Passing Parameters to a function via a 2d object array

I'm trying to write a neat little generic Sql method that will accept a SQL Query and a list of parameters, and return a result. 我正在尝试编写一个简洁的通用Sql方法,该方法将接受SQL查询和参数列表,并返回结果。 I want to keep it neat enough that I can call it using one line from any other code. 我想保持它整洁,以至于可以使用其他任何代码中的一行来调用它。

Is there any really awesomely neat way of doing this? 有没有真的很棒的方法呢? I don't want to create all the SqlParameters in the calling code, and I don't want to have to pass and split a string. 我不想在调用代码中创建所有SqlParameters,也不想传递和拆分字符串。 In the past I've used a string[] array and accepted every odd member as a parameter name and every even as a param value but that's too easy to screw up when calling the method. 过去,我使用string []数组,并接受每个奇数成员作为参数名称,每个偶数作为参数值,但这在调用方法时很容易搞砸。

Ideally I'd love to do just this: 理想情况下,我很想这样做:

Data.SQL("Select * from Table where my_id = @my_id", { my_id = 1 });

I know that's a little unrealistic, So I tried this: 我知道这有点不现实,所以我尝试了以下方法:

Data.SQL("Select * from Table where my_id = @my_id", new Object[,]{ { "my_id", 1 } });

However when I try and handle that on the other end, I get nothing but trouble: 但是,当我尝试在另一端进行处理时,除了麻烦之外,我什么也没有得到:

public static Object SQL(String command, Object[,] parameters = null){
     [ ... reusable SQL code here... ] 

     foreach(Object[] p in parameters){
            cmd.Parameters.Add(new SqlParameter(p[0].ToString(), p[1].ToString());
     }
}

Looks fine, but throws an error on the foreach statement 看起来不错,但在foreach语句上引发错误

foreach (Object[] p in parameters)

Unable to cast object of type 'System.String' to type 'System.Object[]' 无法将类型为“ System.String”的对象转换为类型为“ System.Object []”的对象

But I didn't pass it an array of System.String. 但是我没有将传递给System.String数组。 What I passed was a 2D System.Object[]! 通过的是2D System.Object []! Wasn't it? 是不是

Maybe this is just some small code problem, something stupid I'm doing wrong. 也许这只是一些小代码问题,但我做错了一些愚蠢的事情。 It usually is. 通常是这样。 But I'm figuring you guys know some even neater way to do the above. 但是我想你们知道做上述事情的一些更精巧的方法。

Ideally I'd love to do just this: 理想情况下,我很想这样做:

 Data.SQL("Select * from Table where my_id = @my_id", { my_id = 1 }); 

I know that's a little unrealistic, 我知道这有点不现实,

Well, in exactly that form, yes... but try this instead: 好吧, 正是以这种形式,是的……但是试试看:

Data.SQL("Select * from Table where my_id = @my_id", new { my_id = 1 });

That will use an anonymous type for the argument, which you can examine by reflection. 这将对参数使用匿名类型 ,您可以通过反射进行检查。 You probably only need a single parameter (ie it would be SQL(string sql, object parameters) ) because you would pass multiple parameters in a single object: 您可能只需要一个参数(即它将是SQL(string sql, object parameters) ),因为您将在一个对象中传递多个参数:

Data.SQL("Select * from Table where my_id = @my_id and name = @name",
         new { my_id = 1, name = "Jon" });

More alternatives: 更多选择:

  • If you're using C# 4, you might find dynamic typing useful; 如果您使用的是C#4,则可能会发现动态类型很有用。 look at what Massive does for example. 例如,看看Massive的功能。
  • As mentioned by Ray, you could pass in a Dictionary<string, object> ; 如Ray所述,您可以传入Dictionary<string, object> ; again, C# 3 makes this easier than otherwise: 再次,C#3使此操作比其他方法更容易:

     Data.SQL("...", new Dictionary<string, object> { { "my_id", 1 }, { "name", "Jon" }}); 

EDIT: As for the exact problem you're running into: you need to understand the difference between a rectangular array (eg Object[,] ) and a jagged array (eg Object[][] ). 编辑:至于您遇到的确切问题:您需要了解矩形数组(例如Object[,] )和锯齿状数组(例如Object[][] )之间的区别。 The latter is an array of arrays, which is how you're trying to use the parameter, but it's really only a rectangular array. 后者是一个数组数组,这就是您尝试使用该参数的方式,但实际上它只是一个矩形数组。 Changing your parameter type to Object[][] may well fix that immediate problem - but personally I'd move to one of the approaches above. 将参数类型更改为Object[][]可能会解决当前的问题-但就我个人而言,我将转向上述方法之一。 I'd also try to avoid making everything into a string, by the way. 顺便说一句,我也尽量避免将所有内容变成字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM