2017-05-03 87 views
0

我是Visual Basic的新手,我正在上一學期參加Visual Basic課程以獲得樂趣。我無法獲得datagridview來連接到我的導師給我的.MDF SQL Server數據庫文件。我在sConnection.ConnectionString =「」上得到了一個空的異常錯誤,我對此感到茫然。在datagridview中打開SQL Server MDF文件

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    sConnection.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jkome\Desktop\Kayaks.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
    sConnection.Open() 

    sCommand.CommandText = "Select * from KayakTypes;" 
    sReader = sCommand.ExecuteReader() 
    sTable.Load(sReader) 
    DGVkyaks.DataSource = sTable 
+0

嗯,首先:你有SQL Server的默認設置您的PC('SQLEXPRESS'的實例名稱)** **快速安裝? –

回答

0

嘗試了這一點

Imports System.Data.SqlClient 
Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim sTable As New DataTable 
     Dim connectionString As String = 
      <Text> 
       Data Source=(LocalDB)\v11.0; 
       AttachDbFilename=C:\Users\Jkome\Desktop\CustomerDatabase.mdf; 
       Integrated Security=True; 
       Connect Timeout=30 
      </Text>.Value 

     Using cn As New SqlConnection With {.ConnectionString = connectionString} 
      Using cmd As New SqlCommand With 
       { 
        .CommandText = "Select * from KayakTypes", 
        .Connection = cn 
       } 

       cn.Open() 
       sTable.Load(cmd.ExecuteReader) 

      End Using 
     End Using 

     DGVkyaks.DataSource = sTable 

    End Sub 
End Class 
相關問題