简体   繁体   中英

Why this code is not working in VS2005

I want to use array of SQl Parameters.When i searched over net I got very usefule data at below link. sqlParameters Array in VB.Net .VS2005 shows syntax error near NEW.

Code is as below

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}
    }

Your code uses object initializers which where introduced in VB.Net 9.0 (Visual Studio 2008); hence you can't use them in VB.Net 8.0 (Visual Studio 2005).

You have to do it the verbose way, eg:

Dim parameters(3) As SqlParameter

Dim p As SqlParameter
p = New SqlParameter("@first_name", SqlDbType.VarChar, 50)
p.Value = "john"
parmaters(0) = p

p = New SqlParameter("@last_name", SqlDbType.VarChar, 50)
p.Value = "doe"
parmaters(1) = p

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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