VB.Net 中的 sqlParameters 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16070932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:18:15  来源:igfitidea点击:

sqlParameters Array in VB.Net

arraysvb.netsqlparameter

提问by Fabio Belz

I'm trying to create a typed-sized parameters array in VB.Net:

我正在尝试在 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

但是 VS 说:未声明 Value'。由于其保护级别,它可能无法访问

What's wrong with the code above?

上面的代码有什么问题?

Thanks!

谢谢!

回答by Kenneth

You need to use the VB syntax for object initializers:

您需要对对象初始值设定项使用 VB 语法:

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