I am working on my creating my first database change with C# and ado.net. This code is my input. I want to save the input to a sql database. Please show me what and where to enter the Connection String and how to use the DataReader to place the input variable in a specific row and column. I think this will help me get going with C# and my project.
Console.WriteLine("What is the location of X?");
string XLoc = Console.ReadLine();
double XLocation = Convert.ToDouble(XLoc);
Console.WriteLine("What is the location of Y?");
string YLoc = Console.ReadLine();
double YLocation= Convert.ToDouble(YLoc);
Console.WriteLine("What is the location of Z?");
string ZLoc=Console.ReadLine();
double ZLocation= Convert.ToDouble(YLoc);

Amit MohantyRank #112 · 20K reputationPosted Oct 30, 2022, 5:31 AM EDT
Add the below query after your code. Change your server name and database name in connectionString.
string connectionString = @"Data Source=YourServerName; Initial Catalog=YourDatabaseName; Integrated Security=SSPI"; string sqlQuery = "INSERT INTO YourTableName (XLocation, YLocation, ZLocation) VALUES (@XLocation, @YLocation, @ZLocation)"; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection); sqlCommand.Parameters.AddWithValue("@XLocation", XLocation); sqlCommand.Parameters.AddWithValue("@YLocation", YLocation); sqlCommand.Parameters.AddWithValue("@ZLocation", ZLocation); sqlCommand.ExecuteNonQuery(); sqlCommand.Dispose(); }