2013-04-06 2 views
1

좋은 하루였습니다. 조언 해주십시오
cmd 변수에 커서를 올리면 "Declaration Expected"라는 오류 메시지가 나타나는 이유는 무엇입니까? 어떻게해야합니까?! .. 코드가 아래에 나타납니다.신고 신고 예상

Imports System.Data.Sqlclient 
Imports System.Configuration 

Partial Class _Default 
    Inherits Page 
    Private Shared Connectionstr As String ="DataSource=localhost;initialCatalog=Orders;Integrated Security=true" 
    Dim conn As SqlConnection = New SqlConnection(Connectionstr) 
    Dim cmd As SqlCommand = conn.CreateCommand() 
    cmd.CommandText="SELECT * FROM dbo.Customers" 
End Class 
+0

무엇을해야합니까? 뭐하고 싶니? – Oded

답변

1

속성, 기능 또는 메소드 외부에서 변수 명령을 사용하려고했습니다.

partial 클래스 _Default 상속 페이지

Private Sub DoSomethingWithCustomers() 
    Dim conn As SqlConnection = New SqlConnection(Connectionstr) 
    Dim cmd As SqlCommand = conn.CreateCommand() 
    cmd.CommandText = "SELECT * FROM dbo.Customers" 

    conn.Open() 
    Dim dr = cmd.ExecuteReader() 
    ' Do something with the data returned . . . 
    ' ... 
    ' Now Cleanup: 
    conn.Close() 
    cmd.Dispose() 
    conn.Dispose() 
End Sub 

상기 데이터를 배치함으로써 개선 될 수있다 : 적어도, 이는 데이터와 함께 원하는 작업을 수행하는 방법 (부)에 명령을 배치하려고 당신을 위해 관리되지 않는 리소스의 적절한 폐기 처리 블록을 사용하여 객체에 액세스 : 그 너머

Private Sub DoSomethingBetterWithCustomers() 
    Dim SQL As String = "SELECT * FROM dbo.Customers" 
    Using conn As New SqlConnection(Connectionstr) 
     Using cmd As New SqlCommand(SQL, conn) 
      conn.Open() 
      Dim dr = cmd.ExecuteReader() 
      ' Do something with the data returned 
      ' . . . 
      dr.Close() 
     End Using ' Using Block takes carre of Disposing SqlCommand 
    End Using ' Using Block takes care of Closing and Disposing Connection 
End Sub 

, 정확하게, 당신은 당신의 코드가 뭘 하려는지 알고 어렵다, 그래서 두 가지 예는 위이다 정말로, 정말로 기본적이고 일반적인 것입니다.