Answer the question
In order to leave comments, you need to log in
C# string concatenation?
how can I insert many values into a string so as not to fence this
sql = @"
UPDATE sc_post16.t_trains
SET scale_id = " + e
+",f_time = " + date.ToString("yyyy-MM-dd HH:mm:ss")
+",w_number =" + name[1]
+",brutto = " + name[2]
+ ",tara = " + name[3]
+ ",loadnorm = " + name[4]
+ ",netto = " + name[5]
+ ",overload = " + name[6]
+ ",cargo = " + name[7]
+ ",table_netto = " + name[8]
+ ",from_station = " + name[9]
+ ",to_station = " + name[10]
+ ",speed = " + name[11]
+ ",num_invoice = " + name[12]
+ ",operator_name = " + name[13]
+ ",smena = " + smen
+",WHERE w_number = " + name[1]
+ ",AND f_time = " + date.ToString("yyyy - MM - dd HH: mm: ss")
Answer the question
In order to leave comments, you need to log in
Use command options instead of concatenation.
var commandText = "UPDATE Sales.Store SET Demographics = @demographics WHERE CustomerID = @ID;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("@ID", customerID);
command.Parameters.AddWithValue("@demographics", demoXml);
connection.Open();
command.ExecuteNonQuery();
}
string interpolation$"Name = {name}, hours = {hours:hh}"
1) The easiest way is String.Format.
Write
2) Slightly smarter than StringBuilder. Sequential Append of each piece of your request.
3) Parameterized query with a variable like "@col1", "@col2", etc. and already in SQLCommand to transfer values of these variables.
4) A stored procedure that accepts a set of variables and executes such a query.
5) ORM like EntityFramework
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question