-1

I am developing a C# Windows Forms application. I am reading data from XML which is coming from a web API and storing that XML data into a SQL Server database.

I am able to achieve this. But my only concern is, in a production environment, this application will be in service and there will > 100 000 data to synchronize from XML to SQL Server database. Can anyone review my code and tell me if it is an efficient way, or if it is not please suggest the changes.

Because it's the first time I am working on the APIs and even in coding I am a fresher.

Done everything and able to achieve my requirement. But I need a code review of mine from experts.

public void save_vendor_info()
{
    SqlConnection con = new SqlConnection(@"server=localhost;Database=TEST;integrated security=true");
    con.Open();

    DataTable dt = new DataTable();

    XmlDocument doc = new XmlDocument();
    doc.Load("https://testdata/api/vendordata");

    try
    {
        XmlNode node = doc.DocumentElement.ChildNodes.Cast<XmlNode>().ToList()[0];

        foreach (XmlNode column in node.ChildNodes)
        {
            dt.Columns.Add(column.Name, typeof(String));
        }

        XmlNode Filas = doc.DocumentElement;

        foreach (XmlNode Fila in Filas.ChildNodes)
        {
            List<string> Valores = Fila.ChildNodes.Cast<XmlNode>().ToList().Select(x => x.InnerText).ToList();

           SqlCommand cmd = new SqlCommand(("IF NOT EXISTS (Select Vendorcode From Vendors where Vendorcode = @Vendorcode) INSERT INTO Vendors VALUES (@Vendorcode, @STCD3, @Name1, @Name2, @Name3, @street1, @street2, @street3, @city1, @city2, @city3, @state1, @state2, @state3, @zip1, @zip2, @zip3, @countrycode, @BusinessUnitId, @VATID, @nationalVATID, @IBAN, @BankAccount, @BankCode)"), con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@Vendorcode", Valores[0]);
            cmd.Parameters.AddWithValue("@STCD3", Valores[1]);
            cmd.Parameters.AddWithValue("@Name1", Valores[2]);
            cmd.Parameters.AddWithValue("@Name2", Valores[3]);
            cmd.Parameters.AddWithValue("@Name3", Valores[4]);
            cmd.Parameters.AddWithValue("@street1", Valores[5]);
            cmd.Parameters.AddWithValue("@street2", Valores[6]);
            cmd.Parameters.AddWithValue("@street3", Valores[7]);
            cmd.Parameters.AddWithValue("@city1", Valores[8]);
            cmd.Parameters.AddWithValue("@city2", Valores[9]);
            cmd.Parameters.AddWithValue("@city3", Valores[10]);
            cmd.Parameters.AddWithValue("@state1", Valores[11]);
            cmd.Parameters.AddWithValue("@state2", Valores[12]);
            cmd.Parameters.AddWithValue("@state3", Valores[13]);
            cmd.Parameters.AddWithValue("@zip1", Valores[14]);
            cmd.Parameters.AddWithValue("@zip2", Valores[15]);
            cmd.Parameters.AddWithValue("@zip3", Valores[16]);
            cmd.Parameters.AddWithValue("@countrycode", Valores[17]);
            cmd.Parameters.AddWithValue("@BusinessUnitId", Valores[18]);
            cmd.Parameters.AddWithValue("@VATID", Valores[19]);
            cmd.Parameters.AddWithValue("@nationalVATID", Valores[20]);
            cmd.Parameters.AddWithValue("@IBAN", Valores[21]);
            cmd.Parameters.AddWithValue("@BankAccount", Valores[22]);
            cmd.Parameters.AddWithValue("@BankCode", Valores[23]);

            cmd.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 7
    I'm voting to close this question as off-topic because it belongs on https://codereview.stackexchange.com/ – Salah Akbari Aug 10 '19 at 05:08
  • You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s Aug 10 '19 at 07:00
  • Using SQL Client class will be slower than using Entity. If speed isn't an issue than stay with current code. – jdweng Aug 10 '19 at 08:49
  • The most important small change is to use a transaction to batch the changes. Otherwise SQL Server will commit after each row. A bigger change to really speed up the load would be to stage the data with SqlBulkCopy and MERGE it into the target table. – David Browne - Microsoft Aug 10 '19 at 10:01

1 Answers1

0

some things are jumping to mind.

  1. the communication of the XML over the network and memory & GC is going to hurt, specially talking about the amount of data you claim to send. GC will stop the process to release memory

  2. why use the data table?

  3. reuse the SQL connection make sure pooling is on

  4. prepare the SqlCommand and only assign the parameter values when you are getting ready to execute it.

  5. use a stored procedure

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Walter Verhoeven
  • 3,867
  • 27
  • 36