user6927546 user6927546 - 1 month ago 15
C# Question

Change Comma to dot for all my inserts

I'm using this code for insert from Excel to SQL. I want every insert in my query to have comma replaced by dot.

SqlCommand cmd2 = new SqlCommand("insert into Market (Barcode,Name,Code,TemporaryStock,VatcodeID,Unit,NetValue,DiscountValue,UnderlyingValue,VatValue,TotalValue)
select * from OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=" + textBox1.Text + "', 'SELECT * FROM [datasheet1$H:R]')", con);

cmd2.ExecuteNonQuery();


Should I use global System.Globalization.CultureInfo?

Answer

For replacing chars/strings in another string use the string.Replace method.

string str = "insert into Market(Barcode,Name,Code,TemporaryStock,VatcodeID,Unit,NetValue,DiscountValue,UnderlyingValue,VatValue,TotalValue) select * from OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=" + textBox1.Text + "', 'SELECT * FROM [datasheet1$H:R]')";

SqlCommand cmd2 = new SqlCommand(str.Replace(',', '.'), con);
cmd2.ExecuteNonQuery();

You can find detailed info about it here.

Comments