views:

23

answers:

1

How can i write text field via ",". i try to write rext script with ListDictionary:

ld parameters: a,b,c,d i need : Text=a,b,c,d

  void InsertDataToSql(ListDictionary ld, string TableName)
        {
            string Text = "insert into ENG_" + TableName + " VALUES(";

            string AddText = String.Join(ld, ', ');
A: 

Ignoring the hazards of manually building an SQL statement, you should be able to create the desired string as follows:

string addText =
    string.Join(", ", ld.Cast<DictionaryEntry>()
                        .Select(de => Escape(de.Key) + "=" + Escape(de.Value))
                        .ToArray());

Example:

ListDictionary ld = ListDictionary
{
    { "Key1", "Value1" },
    { "Key2", "Value2" },
    { "Key3", "Value3" },
};

then the Select in the code above would transform this to

string[] temp = new string[]
{
    "Key1=Value1",
    "Key2=Value2",
    "Key3=Value3",
};

and string.Join would join these strings together to

string addText = "Key1=Value1, Key2=Value2, Key3=Value3";

But you really should use a library that does the database access for you. Manually building SQL statements is error-prone and can be even dangerous .

dtb