简体   繁体   English

VB.NET声明错误

[英]VB.NET Declaration error

I am having some trouble in some Visual Basic code where although I have declared a variable, when I try to give it a value, Visual Studio returns an error saying that the variable hasn't been declared. 我在一些Visual Basic代码中遇到了一些麻烦,尽管我已经声明了一个变量,但是当我尝试给它赋值时,Visual Studio返回一个错误,指出未声明该变量。 Here is the block of code: 这是代码块:

Private Sub chkbox_ta_CheckedChanged(sender As Object, e As EventArgs) Handles chkbox_ta.CheckedChanged
    Dim query As String = "SELECT * FROM [Hiragana List] WHERE Pronunciation='Ta';"
    Dim instruction As SqlCommand (query, connection)
    Dim da As New SqlDataAdapter
    da.SelectCommand = instruction
    da.Fill(HiraganaList)
End Sub

The error is thrown up by the 'instruction' variable and Visual Studio hasn't provided any solutions. 该错误由“指令”变量引发,并且Visual Studio未提供任何解决方案。 In addition to this, the query argument within the instruction variable returns the error 'Array bounds cannot appear in type specifiers'. 除此之外,指令变量中的查询参数返回错误“数组边界不能出现在类型说明符中”。 I am still getting used to working with SQL in VB and any explanation which would teach me how to avoid these errors would be very helpful. 我仍然习惯于在VB中使用SQL,任何可以教会我如何避免这些错误的解释都将非常有帮助。

Wrong syntax in declaration and initialization of the SqlCommand. SqlCommand的声明和初始化中的语法错误。
The right syntax is one of the following: 正确的语法是以下之一:

Dim instruction As SqlCommand = new SqlCommand(query, connection)

or 要么

Dim instruction As New SqlCommand (query, connection)

or just 要不就

Dim instruction = new SqlCommand(query, connection)

The Dim Statement has numerous variations the should be studied carefully (especially in the early days with the language) 昏暗的陈述有很多变化,应仔细研究(尤其是在使用该语言的早期)

Data types (string, integer, date) do not need a "new" declaration. 数据类型(字符串,整数,日期)不需要“新”声明。 But when you define something that is a class (Like SqlCommand or one you create yourself) it will need to be initialized with "new". 但是,当您定义某个类(例如SqlCommand或您自己创建的类)时,则需要使用“ new”进行初始化。

Syntax examples from Steve's earlier post 史蒂夫(Steve)早期文章中的语法示例

Dim instruction As SqlCommand = new SqlCommand(query, connection)
Dim instruction As New SqlCommand (query, connection)
Dim instruction = new SqlCommand(query, connection)

Some links that might help out: 一些链接可能会有所帮助:

http://msdn.microsoft.com/en-us/library/47zceaw7.aspx http://msdn.microsoft.com/en-us/library/47zceaw7.aspx

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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