Hi I am trying my first windows application. I have created a connection string in my app.config. I have tried some test coding to see if my connection works prior to completing the coding for textbox/combobox insertions to my .sdf (sqlce) file.
Here is the code I used to test the connection:
private void btnInsertTestCert_Click(object sender, EventArgs e){
SqlCeConnection conn = null; string cnString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; try{
conn =
new SqlCeConnection(cnString);conn.Open();
SqlCeCommand cmd = conn.CreateCommand();cmd.CommandText =
"INSERT INTO tblTestCert (HoseNumber, Customer, HoseType, TestedTo)Values('1', 'Albion', 'Chemical', '225')";cmd.ExecuteNonQuery();
}
finally{
conn.Close();
}
}
I assume the connection works fine as I get no error, yet when I view table data there is no record in the database??
Yet if I click the click event button twice I get a duplicate value error
Any idea what I'm doing wrong???
GraemePosted Dec 16, 2007, 6:26 AM
Hi Brandon
Switched to Access db using your coding and it worked fine, so at least I know its as you said and its only the embeddded file not being updated.
Thanks for your help :)
Posted Dec 14, 2007, 10:52 AM
Try implementing it and checking the local data source, not the embedded one. You will probably see the changes stick :)
Ryan AlfordPosted Dec 14, 2007, 9:56 AM
GraemePosted Dec 13, 2007, 5:27 PM
Hi Brandon
I'm still not quite understanding this, I have taken a look at the link you recommened Lesson 3 was of great interest on the matter.
Unfortunately it doesnt really cover the embedded source file connection using your commit and transaction methods.
I have tried your example but I am having no luck still??
Could you explain what the first argument "COMMIT" is for.
Thank you for your help on this matter
Graeme
Posted Dec 12, 2007, 5:18 PM
Because if you are checking the embedded database, this method of changing its values will not work. What you doing here when you create database commands and pass them via command string is your creating a connection to a remote (not embedded) data source and modifying it. However, using that method, the changes will not stay permanently until you commit them by passing the database a commit command.
The data is being inserted into the database, but since it is a remote source, the changes will not be saved until you commit them. At least thats my take on it and its generally been that way when using Oracle SQL "back in the day", lol.
Essentially, your not even making the changes to the embedded source. In order to do that, check out some information on ADO.NET.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
GraemePosted Dec 12, 2007, 5:03 PM
Hi the database is local, testcert.sdf is actually part of the project.
I am very new to this, but I cant understand why if it is not inserting into the db then why am I getting a duplicate error when I click the event twice ??
Posted Dec 12, 2007, 4:48 PM
Ex:
SqlCeCommand commitCmd = new SqlCeCommand("COMMIT", conn);
commitCmd.ExecuteNonQuery();
Im not sure if you need a transaction to make changes to an SQL database. I am using my OleDb (MS Access) knowledge but I would assume its similar. Also, you may need to create a transaction in order to commit the changes. In that case, try this:
//Not sure if this class exists in your context,
//in mine it is "OleDbTransaction"
SqlCeTransaction trans = conn.BeginTransaction();
//Include the transaction
SqlCeCommand commitCmd = new
SqlCeCommand("COMMIT", conn, trans);
commitCmd.ExecuteNonQuery();
Have you tried adding the database as a data source and manipulating the embedded data source and allowing it to make changes to the database? Unless of course you want to keep the database remote, then you will have to pass commands to it.