Hello.
I am wondering exactly what kind of code is needed when I want to update an sql databse from a windows form.
I have a form with two textfields, and a button called save. When I press that button, I want the information in the textfields to be saved in the database as a new column in the table that these textfields are taken from. So, i want to write the code in the click method of the button.
I guess this is really easy, but I have never tried before, and my book doesnt really explain how to do it. Will really appreciate help :)

Updating SQL database
abruton
Here is an exmaple that I have cut from some code. You might need to play a litte with it as it had been put together using parts of code from different parts of my class ie I don't set the database connection string here but it will get you started.
Make sure you have the Data and SQLClient in the using statements, then create a procedure to take the 2 textbox values. Create a string to hold the SQL trick here is to use the @ sign for the string, this allows the SQL statment to be able to be written on multilple lines in the IDE. Next create a SQL connection. Open the connection, assign the statment to a sql statment and a connection to execute against. Next asign the values of the textboxes to the Parameters. Finally execute with a ExecuteNonQuery, this basically says that you are not expecting a recordset to be returned.
using System.Data.SqlClient;
using
System.Data;public void UpdateManifestTable(DateTime _shipDate,FlightID_filghtid )
{
String _sSQL = @"Update MANIFEST.MANIFEST
SET
FLIGHTID = @FlightID,
PROCESSEDFLAG = 1,
Where Shipdate = @Shipdate'
SqlConnection myConn = new SqlConnection(Data Source=ssadev01;Initial Catalog=train;User ID=bob;Password=hithere;Persist Security Info=True
) ;myConn.Open();
//set up the SQLcommand for the datatreader
SqlCommand _sqlCommand = new SqlCommand(_sSQL, myConn); //Link with the TNTNumber from the datagrid dataset_sqlCommand.Parameters.AddWithValue(
"@SHIPDATE", _shipDate);_sqlCommand.Parameters.AddWithValue("@FlightID", _flightID);
_sqlCommand.ExecuteNonQuery();
myConn.Close();
}
ddwinter
Easiest way is to use DataSet, DataAdapter, WinForms data binding things.
Manual way is a little harder - open connection, create INSERT command, run it, handle errors.
If you need more details - ask.