03. oktober 2008 - 17:19Der er
18 kommentarer og 1 løsning
Gemmer ikke data
JEg sidder og leger med windows form programming i VS2008, har lavet en lille "dims" der gemmer data in en database lokalt (MyData.mdf) og viser det så i en listbox.
Men når jeg åbner programmet igen har den ikke gemt noget, den gemmer kun når programmet er åbent, derefter er alt væk?
Har vedhæftet filen: Imports System.Data Imports System.Data.SqlClient Imports System.Data.OleDb
Public Class Form1 ' A data set for use by the form Dim mydataset As DataSet = New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Configure the connection Dim strconn = "Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=minsti.....App_Data\MyData.mdf;User Instance=true;" Dim conn As SqlConnection = New SqlConnection(strconn) ' Create the adapter from the connection Dim adapter As SqlDataAdapter = New SqlDataAdapter(conn.CreateCommand()) adapter.SelectCommand.CommandText = "select * from BrowserData" ' Fill the data set with the Customers table adapter.Fill(mydataset)
' Populate list box PopulateListBox() End Sub
Sub PopulateListBox() ' Clear the list box ListBox.Items.Clear()
' Enumerate cached data Dim row As DataRow For Each row In mydataset.Tables(0).Rows Dim item As String = row("name") & ", " & row("bid") ListBox.Items.Add(item) Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Ask table for an empty DataRow Dim row As DataRow = mydataset.Tables(0).NewRow()
' Fill DataRow with column data row("name") = TextBox1.Text row("date") = DateAndTime.Now
' Add DataRow to the table mydataset.Tables(0).Rows.Add(row)
Public Class Form1 ' A data set for use by the form Dim mydataset As DataSet = New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Configure the connection Dim strconn = "Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=C:\Users\kette\Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplication1\App_Data\MyData1.mdf;User Instance=True;" Dim conn As SqlConnection = New SqlConnection(strconn) ' Create the adapter from the connection Dim adapter As SqlDataAdapter = New SqlDataAdapter(conn.CreateCommand()) adapter.SelectCommand.CommandText = "select * from BrowserData" ' Fill the data set with the Customers table adapter.Fill(mydataset)
' Populate list box PopulateListBox() End Sub
Sub PopulateListBox() ' Clear the list box
' Enumerate cached data Dim row As DataRow For Each row In mydataset.Tables(0).Rows Dim item As String = row("name") & ", " & row("bid") ListBox.Items.Add(item) Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Ask table for an empty DataRow Dim row As DataRow = mydataset.Tables(0).NewRow()
' Fill DataRow with column data row("name") = TextBox1.Text row("date") = DateAndTime.Now
' Add DataRow to the table mydataset.Tables(0).Rows.Add(row) mydataset.AcceptChanges() ' Update list box
Det er som om at den ikke gemmer data i databasen fordi når listboxen viser det gemte data, viser den ikke BID, som har auto_increment sat op, så jeg har mine anelser.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.