Unable to insert record, unknown sqlceconnection error?

B

Butt Chin Chuan

I want to insert record into my SqlCe database. However, I
got an unknown error message. The breakpoint is pointed to
my consql.open statement. I followed the msdn example
about the datasource and noticed that it is different from
my usual connection string. This is my usual connection
string:
Dim cn As SqlCeConnection = New SqlCeConnection( _
"DataSource=\My Documents\mydatabase.sdf")

My connection string (after followed MSDN) is as follows:
Dim cn As String = "Data Source = mydatabase.sdf;"

Below is my code:

Dim sql As String

sql = ""
sql = "INSERT INTO tbAcademicQualification "
sql = sql + "(MemberID, Degree, Field, Year_, "
sql = sql + "InstitutionName, InstitutionPlace) "
sql = sql + "VALUES ('"
sql = sql + CStr(cMemberID) + "', '"
sql = sql + CStr(txtDegreeDetail.Text) + "', '"
sql = sql + CStr(txtFieldDetail.Text) + "', '"
sql = sql + CStr(txtYearDetail.Text) + "', '"
sql = sql + CStr(txtInstitutionNameDetail.Text) + "', '"
sql = sql + CStr(txtInstitutionPlaceDetail.Text) + "') "

Dim cn As String = "Data Source = mbkms.sdf;"
Dim conSql As New SqlCeConnection(cn)
Dim cmd As New SqlCeCommand
cmd.Connection = conSql
cmd.CommandText = sql
conSql.Open()
cmd.ExecuteNonQuery()
conSql.Close()

I have a few questions here:
1) What is the actual connection string here?
2) Where exactly is "Data Source = mydatabase.sdf;"
actually refers to (location)?
3) My battery charger is spoilt, therefore I'm using the
emulator at the moment.
3i) How can I determine where (in my desktop computer) the
sdf file in the emulator resides?
4) Is the unknown error generated (break) on the
conSql.open statement is due to the location of the sdf
file not correct? If so, how can I set it in the
connection string where it can point to the correct
location?

Any suggestions is greatly appreciated. Thank you.
 
I

Ilya Tumanov [MS]

Do you have the actual database file?

Database has to be created before you could open connection to it.
If you did not created database (by using SQL CE engine class or via SQL CE
query analyzer) it won't work.

Create the database first, open connection later.
Use the path to this database file in the connection string and make sure
file actually exists.

You also would have to create tables and columns in the database before you
could execute the statement.
You could do it via CREATE TABLE statement or in SQL CE query analyzer.

As to your question on how to find emulator files on the desktop, you can
not do this.
Emulator and desktop are separate entities and emulator files are not
mapped into desktop's file system.
Physically these files reside in the emulator image file you could not
change or view directly.

Also, I would suggest you add try/catch block to print out exception
messages so it won't be an 'unknown error'.
Look up the sample code in SqlCeException class description.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top