Can I save variables in a Winforms C# database? | SoloLearn: Learn to code for FREE!


Can I save variables in a Winforms C# database?

How can I save my variables in a database and change its value every time I want from code?

3/14/2018 11:18:51 PM


1 Answer

New Answer


Start thinking in data-objects and methods/functions. A form is just a tool, to get input. So there is a person with a name Then there is a database with a table persons This table has one column "Name" This is the code to insert one row with the name Alice you save the code in the database using a sqlconnection and sqlcommand first_name = "alice"; using (SqlConnection con = new SqlConnection( ConsoleApplication1.Properties.Settings.Default.masterConnectionString)) { con.Open(); try { using (SqlCommand command = new SqlCommand( "INSERT INTO Persons VALUES(@Name)", con)) { command.Parameters.Add(new SqlParameter("Name", first_name)); command.ExecuteNonQuery(); } } catch { Console.WriteLine("Could not insert."); } }