Last active
December 27, 2015 06:29
-
-
Save garyherd/7281572 to your computer and use it in GitHub Desktop.
SQL Server with Data Sets & Data Adapters (i.e. Disconnected mode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //No connection of Connection string creation | |
| //No DataAdapter configuration | |
| //Automatically generates an adapter specifically for working with OUR tables. | |
| //ex. | |
| CustomerDataSetTableAdapters.CustomersTableAdapter cta = new CustomerDataSetTableAdapter; | |
| CustomerDataSet ds = new CustomerDataSet(); | |
| cta.Fill(ds.Customers); | |
| //Do stuff with dataset | |
| //ex looping throw the rows | |
| foreach (CustomersDataSet.CustomersRow customer in ds.Customers.Row) | |
| { | |
| //do stuff | |
| } | |
| //ex: Insert new record | |
| Customers.DataSet.CustomersRow newCustomer = ds.Customers.NewCustomersRow(); | |
| newCustomer.FirstName = "Brian"; | |
| //etc. | |
| ds.Customers.AddCustomersRow(newCustomer); | |
| //ex. Update record | |
| ds.Customers[0].FirstName = "Robert"; | |
| //ex. Delete record | |
| ds.Customers[1].Delete(); | |
| //update changes | |
| cta.Update(ds); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Data; | |
| using.System.Data.SqlClient; | |
| //Create connection | |
| SqlConnection myConnection = new SqlConnection(connectionString); | |
| myConnection.Open(); | |
| SqlCommand myCommand = myConnection.CreateCommand(); | |
| myCommand.CommandType = CommandType.Text; | |
| myCommand.CommandText = "SQL Statement"; | |
| SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand); | |
| DataSet myDataSet = new DataSet(); | |
| myDataAdapter.Fill(myDataSet); | |
| myConnection.Close(); | |
| //Do something with the data in the DataSet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Data; | |
| using.System.Data.SqlClient; | |
| //Create connection | |
| SqlConnection myConnection = new SqlConnection(connectionString); | |
| myConnection.Open(); | |
| SqlDataAdapter myDataAdapter = new SqlDataAdapter(); | |
| myDataAdapter.SelectCommand = myConnection.CreateCommand(); | |
| myDataAdapter.SelectCommand.Command.Text = "SELECT Statement here"; | |
| myDataAdapter.InsertCommand = myConnection.CreateCommand() | |
| myDataAdapter.InsertCommand.Command.Text = "Insert Statement here"; | |
| myDataAdapter.InsertCommand.Parameters.Add("@Parameter1", SqlDbType.TypeHere, int size, Column name) //note Add has four override methods | |
| //more parameters as needed | |
| myDataAdapter.UpdateCommand = myConnection.CreateCommand() | |
| myDataAdapter.UpdateCommand.Command.Text = "Update Statement here"; | |
| myDataAdapter.UpdateCommand.Parameters.Add("@Parameter1", SqlDbType.TypeHere, size, Column name) //note Add has four override methods | |
| //more parameters as needed | |
| myDataAdapter.DeleteCommand = myConnection.CreateCommand(); | |
| myDataAdapter.DeleteCommand.Command.Text = "Delete Statement here"; | |
| myDataAdapter.UpdateCommand.Parameters.Add("@Parameter1", SqlDbType.TypeHere, size, Column name) //note Add has four override methods | |
| //more parameters as needed | |
| DataSet myDataSet = new DataSet(); | |
| myDataAdapter.Fill(myDataSet); | |
| myConnection.Close(); | |
| //Make some modifications to the DataSet's Data | |
| //Insert | |
| DataRow newDataRow = myDataSet.Tables[0].NewRow(); | |
| newDataRow["ColumnName"] = "Value"; | |
| //more DataRows as needed | |
| myDataSet.Tables[0].Rows.Add(newDataRow); | |
| //Update | |
| myDataSet.Tables[0].Rows[0]["ColumnName to update"] = "New field value here"; | |
| //Delete | |
| myDataSet.Tables[0].Rows[RowNumber].Delete(); | |
| //Connect back up, commit the changes | |
| //It will connect on its own, no need to explicitly re-open the connection | |
| myDataAdapter.Update(myDataSet); | |
| //Do other stuff with data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment