问题
I'm trying to create a typed-sized parameters array in VB.Net:
Dim parameters() As SqlParameter = New SqlParameter() _
{
New SqlParameter("@first_name", SqlDbType.VarChar, 50) {Value = "john"},
New SqlParameter("@last_name", SqlDbType.VarChar, 50) {Value = "doe"},
New SqlParameter("@age", SqlDbType.Int) {Value = 18},
New SqlParameter("@id", SqlDbType.Int) {Value = 123}
}
But VS says: Value' is not declared. It may be inaccessible due to its protection level
What's wrong with the code above?
Thanks!
回答1:
You need to use the VB syntax for object initializers:
Dim parameters() As SqlParameter = New SqlParameter() _
{
New SqlParameter("@first_name", SqlDbType.VarChar, 50) With { .Value = "john"},
New SqlParameter("@last_name", SqlDbType.VarChar, 50) With { .Value = "doe"},
New SqlParameter("@age", SqlDbType.Int) With { .Value = 18},
New SqlParameter("@id", SqlDbType.Int) With { .Value = 123}
}
来源:https://stackoverflow.com/questions/16070932/sqlparameters-array-in-vb-net