简体   繁体   中英

VB.NET. How to get data from sdf database?

We are developing windows form app. We have local database which is sdf file. We added reference to our project: SQLServerCe.dll

We are using this code:

Dim strDataSource As String
strDataSource = "Data Source = C:\Users\JOE\Desktop\Class work (7)\Class work\BookIt\BookIt.DBEntity\Library.sdf"
Dim conn As New SqlCeConnection
conn.ConnectionString = strDataSource & ";Password='<password>'"

Dim selectCmd As SqlCeCommand = conn.CreateCommand
selectCmd.CommandText = "SELECT author, title FROM book"

Dim adp As New SqlCeDataAdapter(selectCmd)

Dim ds As New DataSet

' Note: Fill will leave the connection in its original state;
' In this case, the connection was closed so it will be left closed
adp.Fill(ds)

tbAuthor.Text = ds.Tables(0).Rows(0)(0).ToString()
tbTitle.Text = ds.Tables(0).Rows(0)(1).ToString()

This code is not working. This line is giving error:

adp.Fill(ds)

Error: An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll

What we are doing wrong? How to solve this problem?

在创建命令对象之前,请打开数据库连接,即conn.Open()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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