Public Function SaveToDB(ByVal FileName As String) As Boolean
Dim DBName As String = "YourDataBase.sdf"
Dim password As String = "YourPassword"
Dim connectionString = String.Format("Data Source=""{0}""; Password=’{1}’", FileName, password)
'connectionString = String.Format("Data Source=""{0}"";", fileName)
Dim cn1 As New SqlCeConnection(connectionString)
If Not File.Exists(DBName) Then
Dim eng As SqlCeEngine = New SqlCeEngine(connectionString)
eng.CreateDatabase()
Dim sqlString As String = "create table hours (" +
"EmployeeID nvarchar (25) not null, " +
"Reg nvarchar (10) not null, " +
"OT nvarchar (12) not null, " +
"FromDT nvarchar (15) not null, " +
"ToDT nvarchar (15) not null, " +
"constraint pk_EmployeeID primary key (EmployeeID))"
If cn1.State = ConnectionState.Closed Then
cn1.Open()
End If
Dim cmd = New SqlCeCommand(sqlString, cn1)
cmd.ExecuteNonQuery()
End If
Using sr As New FileIO.TextFieldParser(FileName) With {.HasFieldsEnclosedInQuotes = True, .Delimiters = {","}}
Dim cmd1 As SqlCeCommand = cn1.CreateCommand()
cmd1.CommandText = "SELECT * FROM AccountsTable"
While Not sr.EndOfData
Dim Line = sr.ReadFields()
Dim rs As SqlCeResultSet = cmd1.ExecuteResultSet(ResultSetOptions.Updatable Or ResultSetOptions.Scrollable)
Dim rec As SqlCeUpdatableRecord = rs.CreateRecord()
rec.SetString(0, Line(0))
rec.SetString(1, Line(1))
rec.SetString(2, Line(2))
rec.SetString(3, Line(3))
rec.SetString(4, Line(4))
rs.Insert(rec)
End While
End Using
Return True
End Function
PS. I have added a password to the database (on creation) to automatically encrypt the database on creation. Simply remove the password to create an un-encrypted one.
Dim cmd1 As SqlCeCommand = cn1.CreateCommand()
from line 27 to between lines 30 and 31 (i.e after the While Not sr.EndOfData)
Dim cmd1 As SqlCeCommand = cn1.CreateCommand()
cmd1.CommandText = "SELECT * FROM hours"
Public Function SaveToDB(ByVal FileName As String) As Boolean
Dim DBName As String = "YourDataBase.sdf"
Dim password As String = "YourPassword"
Dim connectionString = String.Format("Data Source=""{0}""; Password=’{1}’", FileName, password)
'connectionString = String.Format("Data Source=""{0}"";", fileName)
Dim cn1 As New SqlCeConnection(connectionString)
If Not File.Exists(DBName) Then
Dim eng As SqlCeEngine = New SqlCeEngine(connectionString)
eng.CreateDatabase()
Dim sqlString As String = "create table hours (" +
"EmployeeID nvarchar (25) not null, " +
"Reg nvarchar (10) not null, " +
"OT nvarchar (12) not null, " +
"FromDT nvarchar (15) not null, " +
"ToDT nvarchar (15) not null, " +
"constraint pk_EmployeeID primary key (EmployeeID))"
If cn1.State = ConnectionState.Closed Then
cn1.Open()
End If
Dim cmd = New SqlCeCommand(sqlString, cn1)
cmd.ExecuteNonQuery()
End If
Using sr As New FileIO.TextFieldParser(FileName) With {.HasFieldsEnclosedInQuotes = True, .Delimiters = {","}}
While Not sr.EndOfData
Dim cmd1 As SqlCeCommand = cn1.CreateCommand()
cmd1.CommandText = "SELECT * FROM hours"
If cn1.State = ConnectionState.Closed Then
cn1.Open()
End If
Dim Line = sr.ReadFields()
If Not Line.Count > 4 Then Continue While
Dim rs As SqlCeResultSet = cmd1.ExecuteResultSet(ResultSetOptions.Updatable Or ResultSetOptions.Scrollable)
Dim rec As SqlCeUpdatableRecord = rs.CreateRecord()
rec.SetString(0, Line(0))
rec.SetString(1, Line(1))
rec.SetString(2, Line(2))
rec.SetString(3, Line(3))
rec.SetString(4, Line(4))
rs.Insert(rec)
End While
End Using
Return True
End Function
Dim connectionString = String.Format("Data Source=""{0}""; Password=’{1}’", DBName, password)
I have checked the code and it runs OK, infact, you did not need to change the position of the lines of code I made earlier, but you need to add the check for a connection, i.e you can move these lines to just before the second while loopDim cmd1 As SqlCeCommand = cn1.CreateCommand()
cmd1.CommandText = "SELECT * FROM hours"
If cn1.State = ConnectionState.Closed Then
cn1.Open()
End If