1

使用MSDN支持頁面上的示例代碼(http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.71).aspx),我可以連接到MS SQL服務器並從數據庫檢索數據。這裏是我的代碼:存儲來自MS SQL服務器的數據

Dim conn As New SqlClient.SqlConnection 
    ' TODO: Modify the connection string and include any 
    ' additional required properties for your database. 
    conn.ConnectionString = "" & _ 
     "integrated security=SSPI;data source=SERVER2;" & _ 
     "persist security info=true;User ID=webuser;initial catalog=Binacionales" 
    Try 
     conn.Open() 
     ' Insert code to process data. 
     MessageBox.Show("Succed to connect to database") 
     'Query 
     Dim myCMD As SqlCommand = New SqlCommand("SELECT operaciones.folio FROM operaciones INNER JOIN rutafolios ON operaciones.folio = rutafolios.folio;", conn) 
     Dim myReader As SqlDataReader = myCMD.ExecuteReader() 
     Dim fNextResult As Boolean = True 
     Do Until Not fNextResult 
      Do While myReader.Read() 
       Console.WriteLine(vbTab & myReader.GetInt32(0)) 
      Loop 
      fNextResult = myReader.NextResult() 
     Loop 
    Catch ex As Exception 
     MessageBox.Show("Failed to connect to data source") 
    Finally 

    End Try 

此代碼檢索數據正確,並將其寫入控制檯,現在我需要存儲此數據。我試圖用一個陣列添加以下代碼兩行:

FolioEstatus(i, 0) = i 

i += 1 

寫在控制檯中的數據,但是這給了我下面的錯誤後,立即:「無法連接到數據源」。

這是我的數組如何聲明:

暗淡FolioEstatus(,)作爲整數=新的整數(){}

我怎麼能存儲這些數據?

編輯:

Dim myCMD As SqlCommand = New SqlCommand("SELECT operaciones.folio FROM operaciones INNER JOIN rutafolios ON operaciones.folio = rutafolios.folio;", conn) 
Dim da As New SqlDataAdapter(myCMD) 
Dim dt As New DataTable() 
da.Fill(dt) 
Dim valor As Int32 

現在我能夠訪問存儲與該行的數據:

valor = (dt.Rows(0)("Folio")) 

回答

0

爲什麼不使用我使用SQL適配器解決我的問題數據表?

Dim myReader As SqlDataReader = myCMD.ExecuteReader() 
    Dim FolioEstatus = new DataTable() 
    FolioEstatus.Load(myReader) 

現在你有專門用來處理表格數據從數據庫

For Each row in FolioEstatus.Rows 
     Console.WriteLine("Data from Column ColA = " + row("ColA").ToString() 
     ...... 
    Next 
+0

未來出於某種原因,這一直給我「無法連接到數據源」的錯誤消息的對象的實例。 –