好的,我在这里做错了什么?我正在尝试将一些 vb 的东西重写为 c#(这样我就可以学习一些 c#),但是失败得很惨。
这是代码,我将发布它之后发生的事情:
private object[] _PNs;
internal object[] ParameterNames { set { _PNs = value; } }
private object[] _PVs;
internal object[] ParameterValues { set { _PVs = value; } }
private Enumerations.DataType[] _DTs;
internal Enumerations.DataType[] ParameterDataTypes { set { _DTs = value; } }
protected void PrepareParams(SqlCommand objCmd)
{
try
{
long _DataSize = 0;
int _PCt = _PVs.GetUpperBound(0);
Type _t_dt = _DTs.GetType();
for (int i = 0; i <= _PCt; ++i)
{
if (_t_dt.IsArray)
{
switch (_DTs[i])
{
case 0:
case 33:
case 6:
case 9:
case 13:
case 19:
_DataSize = 8;
break;
case 1:
case 3:
case 7:
case 10:
case 12:
case 21:
case 22:
case 23:
case 25:
_DataSize = Strings.Len(_PVs[i]);
break;
case 2:
case 20:
_DataSize = 1;
break;
case 5:
_DataSize = 17;
break;
case 8:
case 17:
case 15:
_DataSize = 4;
break;
case 14:
_DataSize = 16;
break;
case 31:
_DataSize = 3;
break;
case 32:
_DataSize = 5;
break;
case 16:
_DataSize = 2;
break;
case 15:
break;
}
// here
objCmd.Parameters.Add(_PNs[i], _DTs[i], _DataSize).Value = _PVs[i];
}
else
{
// here
objCmd.Parameters.AddWithValue(_PNs[i], _PVs[i]);
}
}
_PNs = null;
_PVs = null;
_DTs = null;
}
catch (Exception ex)
{
}
}
编辑:随着我现在得到的答案中建议的更改:Cannot implicitly convert int to ...DataType
以及方法Has some invalid arguments
Parameter.Add
所以我假设我的这些类属性被错误地声明了。
我怎样才能解决这个问题?
这是原始的VB代码:
Protected Friend WriteOnly Property ParameterNames() As Object
Set(ByVal value As Object)
_PNs = value
End Set
End Property
Private _PNs As Object
Protected Friend WriteOnly Property ParameterValues() As Object
Set(ByVal value As Object)
_PVs = value
End Set
End Property
Private _PVs As Object
Protected Friend WriteOnly Property ParameterDataTypes() As DataType()
Set(ByVal value As DataType())
_DTs = value
End Set
End Property
Private _DTs As DataType()
Private Sub PrepareParams(ByVal objCmd As Object)
Try
Dim _DataSize As Long
Dim _PCt As Integer = _PVs.GetUpperBound(0)
For i = 0 To _PCt
If IsArray(_DTs) Then
Select Case _DTs(i)
Case 0, 33, 6, 9, 13, 19
_DataSize = 8
Case 1, 3, 7, 10, 12, 21, 22, 23, 25
_DataSize = Len(_PVs(i))
Case 2, 20
_DataSize = 1
Case 5
_DataSize = 17
Case 8, 17, 15
_DataSize = 4
Case 14
_DataSize = 16
Case 31
_DataSize = 3
Case 32
_DataSize = 5
Case 16
_DataSize = 2
Case 15
End Select
objCmd.Parameters.Add(_PNs(i), _DTs(i), _DataSize).Value = _PVs(i)
Else
objCmd.Parameters.AddWithValue(_PNs(i), _PVs(i))
End If
Next
Erase _PNs : Erase _PVs : Erase _DTs
Catch ex As Exception
End Try
End Sub