4
Reply

String.Format method

Nepethya Rana

Nepethya Rana

Jul 5 2017 9:38 AM
220
This is my extension method in C# 
 
public static class SqlCommandExt
{
public static SqlParameter[] AddArrayParameters<T>(this SqlCommand cmd, IEnumerable<T> values, string paramNameRoot, int start = 1, string separator = ", ")
{
var parameters = new List<SqlParameter>();
var parameterNames = new List<string>();
var paramNbr = start;
foreach (var value in values)
{
var paramName = string.Format("@{0}{1}", paramNameRoot, paramNbr++);
parameterNames.Add(paramName);
parameters.Add(cmd.Parameters.AddWithValue(paramName, value));
}
cmd.CommandText = cmd.CommandText.Replace("{" + paramNameRoot + "}", string.Join(separator, parameterNames));
return parameters.ToArray();
}
}
 
I Tried to convert it into VB.net
 
Module StringExtensions
<Extension()>
Public Function AddArrayParameters(Of T)(ByVal cmd As SqlCommand, ByVal values As IEnumerable(Of T), ByVal paramNameRoot As String, Optional ByVal start As Integer = 1, Optional ByVal separator As String = ", ") As SqlParameter()
Dim parameters = New List(Of SqlParameter)
Dim parameterNames = New List(Of String)
Dim paramNbr As Integer = start
For Each value In values
Dim paramName = String.Format("@{0}{1}", paramNameRoot, paramNbr + +)
parameterNames.Add(paramName)
parameters.Add(cmd.Parameters.AddWithValue(paramName, value))
Next
cmd.CommandText = cmd.CommandText.Replace(("{" + (paramNameRoot + "}")), String.Join(separator, parameterNames))
Return parameters.ToArray
End Function
End Module
 
But the line Dim paramName = String.Format("@{0}{1}", paramNameRoot, paramNbr + +)
says expression expected. I think "paramNbr + +" is causing the problem.
Please help me
 

Answers (4)