VB.NET。如何从sdf数据库中获取数据?

时间:2015-04-21 12:43:19

标签: sql vb.net

我们正在开发Windows窗体应用。我们有本地数据库,这是sdf文件。我们添加了对项目的引用:SQLServerCe.dll

我们正在使用此代码:

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()

此代码无效。这一行给出错误:

adp.Fill(ds)

错误:System.Data.SqlServerCe.dll中出现未处理的“System.Data.SqlServerCe.SqlCeException”类型异常

我们做错了什么?如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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