Cannot insert records into a SQL Server CE database

76 views Asked by At

I cannot insert records into a SQL Server CE database. I have read lots of articles but I still did not receive a proper answer for that.

This is my code. Database is located in the bin folder of the project.

Dim strConnection As String

strConnection = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\Barcode_UAT.sdf;Persist Security Info=False"

Dim cn As New SqlCeConnection(strConnection)

If cn.State = ConnectionState.Closed Then
    cn.Open()
End If

Dim cmd As SqlCeCommand

Dim sql As String = "insert into [tbl_Barcodes] ([SerialNo],[ItemId],[Date]) values (@SerialNo,@ItemId,@Date)"

Try
    cmd = New SqlCeCommand(sql, cn)
    cmd.Parameters.AddWithValue("@SerialNo", "12121333")
    cmd.Parameters.AddWithValue("@ItemId", "Item01010")
    cmd.Parameters.AddWithValue("@Date", "2012-2-2")
    cmd.ExecuteNonQuery()
    cmd.Dispose()

Catch sqlexception As SqlCeException
    MessageBox.Show(sqlexception.Message)

Catch ex As Exception
    MessageBox.Show(ex.Message)

Finally
    cn.Close()

End Try
1

There are 1 answers

0
user3416344 On

It seems like you are missing the bin folder in your Data Source location path