Here is everything that I did:
In a visual studio 2013 C# project, I created a service database (.mdf file). Note: I changed the name from Database1.mdf to fghLocalDB.mdf.
I opened this database in the server explorer.
I created 2 tables called Country and CarbonDioxide using the table designer.
I added an entry to the Country table as shown by the Data Table of the Country table.
I did the following to create a DataSet my application can use. I created a Data Source by clicking on the "Project" option on the top menu bar and clicking on the "Add New Data Source ..." option from the drop down.
This is what my project files looked like at this point.
I wrote the following code in the main method thinking that this would be all I need to write to the database.
// Create a connection to the DataSet and TableAdapters that will communicate with our // local database to handle CRUD operations. fghLocalDBDataSet dataSet = new fghLocalDBDataSet(); fghLocalDBDataSetTableAdapters.CountryTableAdapter countryTableAdapter = new fghLocalDBDataSetTableAdapters.CountryTableAdapter(); try { // Insert a row into Country table. EDIT 1 Will comment after first program run. Console.WriteLine(countryTableAdapter.Insert("United States")); // Actually writeback information to the database? // dataSet.AcceptChanges(); EDIT 2 commented this as LeY suggested it was not needed. // EDIT 3 Validation code as suggested by Ley. var dt = new fghLocalDBDataSet.CountryDataTable(); var adapter = new fghLocalDBDataSetTableAdapters.CountryTableAdapter(); adapter.Fill(dt); foreach (var row in dt) { // This does not get executed after a second run of the program. // Nothing is printed to the screen. Console.WriteLine("Id:" + row.Id + "----Name: " + row.Name); } Console.Read(); } catch(SqlException exception){ Console.WriteLine("ERROR: " + exception.ToString()); } Console.ReadLine();
I ran the program and everything seemed fine.
- I opened the tables by right clicking on these tables in the server explorer and pressing "Show Data Table".
- The "United States" row was not added as wanted.
I think it has to do with the connectionstring. I right clicked on my project and opened properties.
Here I made sure the connection string matched that of the local database by looking at the string in the properties of the database. They are the same.
I copied and pasted the actual text for each connection string:
Connection string of project:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\fghLocalDB.mdf;Integrated Security=True
Connection string of actual database (.mdf file):
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\gabriel\Source\Workspaces\Capstone\Sandbox\aduclos\QueryDataMarketConsole\QueryDataMarketConsole\fghLocalDB.mdf;Integrated Security=True
I am assuming |DataDirectory| is equal to C:\Users\gabriel\Source\Workspaces\Capstone\Sandbox\aduclos\QueryDataMarketConsole\QueryDataMarketConsole\fghLocalDB.mdf; since in the picture above when I clicked on the button to expand the Value of the connection string the connection properties window opened up and had this path for the database file name.
My question in a nutshell is does instantiating a DataSet object in the code automatically create a connection to a SQL service-based database for CRUD operations?
If not how do I connect my DataSet object to my sql database so that way I can actually write to the database when using the TableAdapters?
I read the following links:
Insert method of TableAdapter not working?
TableAdapter Insert not persisting data
Use connectionstring from web.config in source code file
Do I need an actual SqlConnection object? and how to I connect this to the DataSet & TableAdapters?