consume an asp.net service and import data into a sql table

0

I have a service which when entering a number of an invoice brings me some fields and information, what I need to do is insert those same fields with the information to a sql table

I am what I have, the moment only brings me the data, but I do not know how to import them into sql

webserverDespacho.IntercambioSW service = new webserverDespacho.IntercambioSW();
        DataTable dDataTable = service.aviso_despacho(TxtFactura.Text);
        DataRow[] currentRows = dDataTable.Select(
        null, null, DataViewRowState.CurrentRows);
        if (currentRows.Length < 1)
            System.Diagnostics.Debug.Write("No Current Rows Found");
        else
        {
            foreach (DataColumn column in dDataTable.Columns)
            System.Diagnostics.Debug.Write("\t{0}", column.ColumnName);
            System.Diagnostics.Debug.WriteLine("\tRowState");
            foreach (DataRow row in currentRows)
            {
                foreach (DataColumn column in dDataTable.Columns)
                    System.Diagnostics.Debug.Write("\t{0}","" + row[column]);
                System.Diagnostics.Debug.WriteLine("\t" + row.RowState);
            }
        }  

This way I'm trying to save the data in the sql table

 string strCon = ConfigurationManager.ConnectionStrings["Sk_DBRadioFrecuencias"].ConnectionString;
    SqlConnection conn = new SqlConnection(strCon);
    SqlCommand comm = new SqlCommand();
    comm.Connection = conn;
    for (int i = 0; dDataTable.Rows.Count > i; i++)
    {
        //insert Query
        comm =  "insert into  SE_FactEmbarque (SE_Factura, SE_Almacen_Despacho, SE_Orden_De_Venta) values ('" + dDataTable.Rows[i]["factura"].ToString().Trim() + "','" +
            dDataTable.Rows[i]["almacen_despacho"].ToString().Trim() + "','" + dDataTable.Rows[i]["ov"].ToString().Trim()+ "')";
    }

But I'm getting an error.

and this is the information that the service brings me

<factura diffgr:id="factura1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<factura>CD-00007</factura>
<almacen_despacho>CDIS</almacen_despacho>
<ov>OV238619</ov>
<oc>57-5776159</oc>
<fechaov>201503101130</fechaov>
<vendido_a>HOMCE33</vendido_a>
<ean_vendido_a>7703670900573</ean_vendido_a>
<cobrar_a>HOMCE33</cobrar_a>
<ean_cobrar_a>7703670900573</ean_cobrar_a>
<despachar_a>HOMCE33</despachar_a>
<ean_despachar_a>7703670900573</ean_despachar_a>
<nombre_despachar_a>HC Santa Marta</nombre_despachar_a>
<articulo>204804</articulo>
<descripcion>Lav S 55x43cm Grafado</descripcion>
<ean_articulo>7707021248048</ean_articulo>
<cantidad_facturada>6</cantidad_facturada>
<cantidad_pendiente>6</cantidad_pendiente>
<peso>2,2</peso>
<volumen>0,003</volumen>
</factura>
    
asked by Eduard Zora 01.04.2017 в 09:15
source

1 answer

0

I would advise you to always use parameters when you arm a query, NEVER concatenate values in a string, it is bad practice.

Something like this

string strCon = ConfigurationManager.ConnectionStrings["Sk_DBRadioFrecuencias"].ConnectionString;
SqlConnection conn = new SqlConnection(strCon);
conn.Open();

string query =  @"insert into SE_FactEmbarque (SE_Factura, SE_Almacen_Despacho, SE_Orden_De_Venta) 
                    values (@factura, @alamacen,  @ordenventa)";

SqlCommand comm = new SqlCommand(query, conn);

foreach (var row in dDataTable.Rows)
{
    comm.Parameters.Clear();
    comm.Parameters.AddWitValue("@factura", row["factura"].ToString().Trim());
    comm.Parameters.AddWitValue("@alamacen", row["almacen_despacho"].ToString().Trim());
    comm.Parameters.AddWitValue("@ordenventa", row["ov"].ToString().Trim());

    comm.ExecuteNonQuery();
}

as you will see the code is ams tidy and readable

    
answered by 03.04.2017 / 12:23
source