我需要一個簡單的程序,它可以添加,編輯,保存,刪除vb.net數據庫中的數據。我已經嘗試過數據集或sqlcommandbuilder,但兩者都不起作用。 請看看我用過的sqlcommandbuilder的代碼,它沒有在數據庫中保存任何東西。 請提供最簡單的方法將數據從數據集或直接從文本框保存到數據庫中。 在此vb.net沒有顯示任何錯誤,但不保存無法保存vb.net數據庫中的數據
'set up a connection string'
Dim connectionstring As String
connectionstring = "Data Source=|DataDirectory|\inventorymanage.sdf;Password='XXX'"
Dim sqlquery As String = "SELECT * FROM ledger"
'create connection'
Dim connaction As SqlCeConnection = New SqlCeConnection(connectionstring)
Try
'open connection'
connaction.Open()
'create data adapter'
Dim da As SqlCeDataAdapter = New SqlCeDataAdapter(sqlquery, connaction)
'create command builder'
Dim commandbuilder As SqlCeCommandBuilder = New SqlCeCommandBuilder(da)
'create dataset'
Dim ds As New DataSet
'fill dataset'
da.Fill(ds, "ledger")
'get datatable'
Dim dt As DataTable = ds.Tables("ledger")
Dim dsnewrow As DataRow
dsnewrow = ds.Tables("ledger").NewRow()
ds.Tables("ledger").Rows.Add(dsnewrow)
With dt
' modify data rows in ledger
.Rows(0).Item(1) = Textbox4.Text
.Rows(0).Item(2) = TextBox5.Text
End With
da.Update(ds, "ledger")
MsgBox("record added")
Catch ex As SqlCeException
MsgBox("you got error" & ex.ToString & vbCrLf)
Finally
'close connection'
connaction.Close()
End Try
使用您的代碼後,我仍然無法保存數據庫中的數據。我正在使用sql server compact3.5。 – 2012-03-13 15:00:04
你看過這裏嗎? http://stackoverflow.com/questions/3223359/cant-get-sql-server-compact-3-5-4-to-work-with-asp-net-mvc-2 – 2012-03-13 15:14:41