D
D
des1roer2016-02-18 10:36:24
C++ / C#
des1roer, 2016-02-18 10:36:24

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

5 answer(s)
A
Artem Voronov, 2016-02-18
@des1roer

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();
    }

D
Dasha Tsiklauri, 2016-02-18
@dasha_programmist

string interpolation
$"Name = {name}, hours = {hours:hh}"

D
Dmitry Kovalsky, 2016-02-18
@dmitryKovalskiy

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

A
Anton Fedoryan, 2016-02-18
@AnnTHony

StringBuilder

G
GavriKos, 2016-02-18
@GavriKos

Use StringBuilder. Or at least string.Format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question