C # List - Insert into the database

0

I have an app that takes information from a TextBox , inserts into a List<> list, and then shows a CheckBoxList . Example:

List<string> quantidade = new List<string>();
for (int i = 0; i < CheckBoxListLinhas.Items.Count; i++)
{
   quantidade.Add(TextBoxQuantidade.Text.ToString());
}
List<string> artigo = new List<string>();
for (int i = 0; i < CheckBoxListLinhas.Items.Count; i++)
{
   artigo.Add(TextBoxArtigo.Text.ToString());
}
List<string> valor = new List<string>();
for (int i = 0; i < CheckBoxListLinhas.Items.Count; i++)
{
   valor.Add(TextBoxValor.Text.ToString());
}
/*List<string> artigo = new List<string>();
  for (int i = 0; i < CheckBoxListLinhas.Items.Count; i++)
  {
    artigo.Add(TextBoxArtigo.Text.ToString());
  }*/

CheckBoxListLinhas.Items.Add(new ListItem("Quantidade: " + TextBoxQuantidade.Text + " Artigo: " + TextBoxArtigo.Text + " Valor: " + TextBoxValor.Text) + " Anexo: " + FileUpload1.FileName.ToString());

And I want to add to the database all the lines inserted in List<> and I already have this example:

SqlCommand sqlInsertList = new SqlCommand("Insert into linhas (quantidade,descricao,valor) VALUES(@quantidade,@descricao,@valor)", sqlConn);
sqlInsertList.Parameters.AddWithValue("@quantidade", );
sqlInsertList.Parameters.AddWithValue("@descricao", TextBoxArtigo.Text);
sqlInsertList.Parameters.AddWithValue("@valor", float.Parse(TextBoxValor.Text, CultureInfo.InvariantCulture.NumberFormat));

sqlConn.Open();                         
sqlTran = sqlConn.BeginTransaction();
sqlInsert.Transaction = sqlTran;        
sqlInsert.ExecuteNonQuery();             
sqlTran.Commit();

What is the easiest way to do this and adapt SQLInsert ?

    
asked by anonymous 15.04.2016 / 13:20

1 answer

0

As I see it, the problem here is that three lists are being created to operate, instead of just one, facilitating abstraction from the Line idea. First you would have to have a class that represents this kind of data.

public class Linha
{
    public int Quantidade { get; set; }
    public string Descricao { get; set; }
    public float Valor { get; set }
}

Using a concrete class you no longer need to loop three times, one for each property of the Line entity.

IList<Linha> linhas = new List<Linha>();

for (int i = 0; i < CheckBoxListLinhas.Items.Count; i++)
{
    linhas.Add(new Linha
    {
        Quantidade = int.Parse(TextBoxQuantidade.Text.ToString(), CultureInfo.InvariantCulture.NumberFormat),
        Descricao = TextBoxArtigo.Text.ToString(),
        Valor = float.Parse(TextBoxValor.Text, CultureInfo.InvariantCulture.NumberFormat)
    });
}

Then to insert all the lines the process becomes much simpler, with only one foreach .

foreach (Linha linha in linhas)
    this.InserirLinha(linha);
}

Consider that InsertLine is your current method for handling only one unit. Modify your code in this method so that it works as follows:

SqlCommand sqlInsert = new SqlCommand("Insert into linhas (quantidade,descricao,valor) VALUES(@quantidade,@descricao,@valor)", sqlConn);

sqlInsert.Parameters.AddWithValue("@quantidade", linha.Quantidade);
sqlInsert.Parameters.AddWithValue("@descricao", linha.Descricao);
sqlInsert.Parameters.AddWithValue("@valor", linha.Valor);

sqlConn.Open();                         
sqlTran = sqlConn.BeginTransaction();
sqlInsert.Transaction = sqlTran;        
sqlInsert.ExecuteNonQuery();             
sqlTran.Commit();

COMMENTS:

  • While looping, you are always accessing the same three fields. I do not know if that was the behavior you really wanted. If there are multiple lines or you want to duplicate this information.
  • I have used the float type for the Value property, but if it is to handle a monetary value the most appropriate type for these operations is the decimal .
  • I do not know how your method is completely inserting a Line , but in the example that was passed the sqlConn is not being closed. Always close connections to bank-related operations (IO, stream, etc.) when you complete your action. If you do not want to use the Close method, these classes generally implement the IDisposable interface, allowing you to Instruction Using , which already does this work for you.
15.04.2016 / 18:10