简体   繁体   English

在不知道类型或不使用混合类型数组的情况下分配SqlParameter.Value

[英]Assigning SqlParameter.Value without knowing type or using mixed type array

I've got a data layer I'm working on that calls into a database three times with three different stored procedures. 我有一个正在处理的数据层,它使用三个不同的存储过程对数据库进行了三次调用。 I initially created three different functions to retrieve three dimensionalities of results. 我最初创建了三个不同的函数来检索结果的三个维度。 The first returns a single value, the second an entire row, and the third a table. 第一个返回单个值,第二个返回整个行,第三个返回表。 They also take in different parameters. 它们还采用不同的参数。 The first takes two varchars, the second two ints, and the last three varchars. 第一个使用两个varchar,第二个使用两个int,最后三个使用varchar。 If I try to get fancy and merge it all down as shown below, am I going to have problems? 如果我尝试花哨并将其全部合并,如下所示,我会遇到问题吗?

public void CallStoredProcedure(string[] astrParams, string strConnectionString, string strStoredProcedure)
{
    int nParams = 0;
    SqlParameter[] asqlParams;
    asqlParams = SqlHelperParameterCache.GetSpParameterSet(strConnectionString, strStoredProcedure);
    foreach (string strParam in astrParams)
    {
        asqlParams[nParams].Value = strParam;
        nParams++;
    }
}

Alternately, can I use an array of mixed data types without know what is in there, and can I assign different types into the same array, replacing elements? 或者,我可以使用混合数据类型的数组而不知道其中有什么,并且可以将不同的类型分配到同一数组中以替换元素吗?

object[] aParams;
string strName = "Joe";
long lngHeight = 180;
object[0] = strName;
object[1] = lngHeight;
CallStoredProcedure(aParams, strConnectionString, "StoredProcedure1")

long lngWeight = 3;
string strItemName = "Bookend";
object[0] = lngWeight;
object[1] = strItemName;
CallStoredProcedure(aParams, strConnectionString, "StoredProcedure2")

And then change the code inside that function to: 然后将该函数中的代码更改为:

foreach (object oParam in astrParams)
{
    asqlParams[nParams].Value = oParam;
    nParams++;
}

Would I need to use ToString() in either or both of these cases? 在这两种情况中的一种或两种情况下,我都需要使用ToString()吗? And if so, does that essentially turn them into the same thing? 如果是这样,这是否实质上将它们变成了同一件事? Like I said, right now I've got three functions that take in all the parameters correctly typed, but I'm trying to get rid of duplicate code if possible. 就像我说的那样,现在我有三个函数可以接受正确键入的所有参数,但是我正在尝试摆脱重复的代码(如果可能)。

You cannot have an array of different data types, you can have an array of objects and cast the individual elements to a specific data type when saving the values to typed variable though. 您不能具有不同数据类型的数组,但是可以具有对象数组,但是在将值保存到类型变量时将单个元素转换为特定数据类型。

Not sure where you want to use ToString() but if its when you are saving a string into an Object array there is no point but if you are saving an object to a string variable then yes you would need to do this: 不确定要在何处使用ToString()但是如果将字符串保存到Object数组时没有意义,但是如果要将对象保存到字符串变量,则需要这样做:

string str = objectArray[0].ToString();

I would avoid this whole mess though, and follow what DJ KRAZE said and add your parameters via Parameter.AddWithValue though. 不过,我会避免整个混乱,并遵循DJ KRAZE所说的,并通过Parameter.AddWithValue添加参数。

I was fairly confused about what I needed to do with my initial question. 我对最初的问题需要做什么感到很困惑。 What I realized is that I don't need to worry about C# types when adding the parameters, since GetSpParameterSet pulls all the information I need into the SqlParameter array, and resets the array every time I call it. 我意识到的是,添加参数时无需担心C#类型,因为GetSpParameterSet会将我需要的所有信息提取到SqlParameter数组中,并在每次调用它时重置该数组。 So my initial code: 所以我的初始代码:

foreach (string strParam in astrParams)
{
    asqlParams[nParams].Value = strParam;
    nParams++;
}

...will just work for my needs. ...将满足我的需求。 There won't be any problem assigning a C# string type into an SQL int type, which was my main concern, since the languages don't have identical types anyway. 将C# string类型分配给SQL int类型不会有任何问题,这是我主要关心的问题,因为这些语言始终没有相同的类型。 I know that I won't have to worry about anything like an "x" going into an int type because all the values I'm using are being pulled from the properly typed columns in other SQL queries already. 我知道我不必担心像“ x”这样的类型会变成int类型,因为我正在使用的所有值已经从其他SQL查询中正确键入的列中提取出来了。 So I won't need any object arrays. 因此,我不需要任何对象数组。

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

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