2017-07-28 58 views
1

我有一个问题,插入数据到sql server 2012
我的问题是,当我保存QuantitaPrezzo值改变了!它可能是什么? QuantitaPrezzo as Money values are from a datagridview我给你留下一个例子,用datagridview用于插入图像和插入数据库的结果。错误查询Sql Server插入,不正确的值格式C#

值范围内的DataGridView:

enter image description here

AFTER INSERT INTO SQL-SERVER:

enter image description here

SqlConnection conn = db.apriconnessione(); 
      SqlTransaction sqlTran = conn.BeginTransaction(); 
      try 
      { 
       //avvio la transazione 

       SqlCommand command = conn.CreateCommand(); 
       command.Transaction = sqlTran; 

       //InserimentoBolla 
       DateTime dataconvertita = Convert.ToDateTime(labelDATADDTMOD.Text); 
       command.CommandText = "SET IDENTITY_INSERT Bolla ON"; 
       command.ExecuteNonQuery(); 
       command.CommandText = "INSERT INTO Bolla(NumeroDDT,IdCantiere,DataDDT,Agente,RagioneSociale,CodiceCliente,RiferimentiInterni,Importo,Destinazione,Filiale,Magazzino,Preparato,Vettore,TipoTrasporto) VALUES('" + labelNUMDDTMOD.Text+"','"+IdCantiere+"',convert(datetime,'"+dataconvertita+"', 103),'" + labelAgenteMOD.Text+"','"+labelRagioneSocialeMOD.Text+"','"+int.Parse(labelCodiceClienteMOD.Text)+"','"+labelRIFInternoMOD.Text+"','"+float.Parse(labelImportoMOD.Text)+"','"+labelDestMOd.Text+"','"+labelFilialeMOD.Text+"','"+labelMagazzinoMOD.Text+"','"+labelPreparatodaMOD.Text+"','"+labelvettoreMOD.Text+"','"+labelTipoTrasportoMOD.Text+"')"; 
       command.ExecuteNonQuery(); 
       command.CommandText = "SET IDENTITY_INSERT Bolla OFF"; 
       command.ExecuteNonQuery(); 
       //fine bolla 

       //inserimento articolo 

       for (int rows = 0; rows < dataGridViewArticoli.Rows.Count; rows++) 
       { 

         string Fornitore = dataGridViewArticoli.Rows[rows].Cells[0].Value.ToString(); 

         string ModelloFornitore = dataGridViewArticoli.Rows[rows].Cells[1].Value.ToString(); 

         string SiglaMetel = dataGridViewArticoli.Rows[rows].Cells[2].Value.ToString(); 

         string CodiceMetel = dataGridViewArticoli.Rows[rows].Cells[3].Value.ToString(); 

         string CodiceInterno = dataGridViewArticoli.Rows[rows].Cells[4].Value.ToString(); 

         string Descrizione = dataGridViewArticoli.Rows[rows].Cells[5].Value.ToString(); 

         float prezzo = float.Parse(dataGridViewArticoli.Rows[rows].Cells[6].Value.ToString()); 
         // MessageBox.Show(" "+prezzo); 
         float quantita = float.Parse(dataGridViewArticoli.Rows[rows].Cells[8].Value.ToString()); 

        // MessageBox.Show("Quantita: "+quantita); 
         command.CommandText = "INSERT INTO ArticoloCantiere(IdCantiere,IdUtente,CodArt,CodMarca,CodiceInterno,ModelloFornitore,Prezzo,Quantita,Fornitore,Importato) VALUES('" + IdCantiere + "','"+u.IdUtente+"','" + CodiceMetel + "','" + SiglaMetel + "','" + CodiceInterno + "','" + ModelloFornitore + "','" + prezzo + "','" + quantita + "','COMET','BOLLA')"; 
         command.ExecuteNonQuery(); 

       } 
       //fine inserimento articolo 

       //conferma delle transazioni con la commit 
       sqlTran.Commit(); 


      } 

      catch (Exception ex) 
      { 
       sqlTran.Rollback(); 
       MessageBox.Show("Errore nell'inserimento "+ex); 
      } 

      conn.Close(); 

      this.DialogResult = DialogResult.OK; 
      this.Close(); 
+0

请问我有什么难以理解你说什么,请你纠正一点你的语法? – Esperadoce

+0

好的一刻.... – riki

+0

尝试:float prezzo = dataGridViewArticoli.Rows [rows] .Cells [6] .Value; – jdweng

回答

1

您插入字符串值用 '' 分隔成money数据类型,这是一个问题。

首先,在SQL Server中,小数点分隔符总是'。',而不是','。

其次,你应该不通过numbers作为strings

这里是重现你的问题的代码:

declare @t table(col money) 
insert into @t values 
('1,44'); 

select * 
from @t; 
---------- 

-- 144,00 

货币数据类型接受与','分离器的输入,但是它把它不作为小数分隔符但是千分位符号

所以要解决它,最好的方法是将数字作为数字传递,最糟糕的事情就是用''替换','。插入前:

declare @t table(col money) 
insert into @t values 
(replace ('1,44', ',', '.')); 

select * 
from @t; 
----- 
---1,44 
+0

我不知道这一点,但是我必须包含的datagridview的值由包含文章的文件加载,如果我尝试用quantita和prezzo替换所有字符串的,我把。它应该工作吗? – riki

+0

是的,完全如此,你应该传递一个点(。)作为小数点分隔符,而不是逗号(,) – sepupic

+0

它完美的作品,如果我在街上找到你我给你一杯咖啡 – riki

0

我建议在加断点:

command.ExecuteNonQuery(); 

,并仔细检查这两个值是正确的:

prezzo and quantita 

编辑:如果值不如预期,请检查SQL数据类型。

我也建议添加SQL参数,否则你的代码很容易受到SQL注入:

INSERT INTO 
     ArticoloCantiere 
     (field1, field2...) 
     VALUES 
     (@value1, @value2...) 

则:

command.Parameters.Add(new SqlParameter("@value1", <somevalue>)); 
command.Parameters.Add(new SqlParameter("@value1", <somevalue>)); 
... 
command.ExecuteNonQuery(); 
+0

我有尝试,这不工作 – riki

+0

你的意思是值仍然不正确? –

+0

当然,我已经更正了我自己的所有值 – riki

0

您需要使用参数化查询。这是一个大交易在安全方面,如果你不这样做,你实际上乞求被黑客攻击。

但这不仅仅关乎安全。参数化查询也可能会修复您的格式问题,因为它们也可以在数据中使用单引号自动解释日期格式和文本值等内容。作为奖励,你通常也会得到(非常小但可测量的)性能提升。

下面是它如何看:

//putting this all in one sql string to execute in one DB call eliminates the need for C# to manage transactions. 
// If you're nervous about it, you can add "BEGIN TRANSACTION" and "COMMIT" statements to the SQL. 
string sql = "SET IDENTITY_INSERT Bolla ON;\n" 
    + "INSERT INTO Bolla (NumeroDDT,IdCantiere,DataDDT,Agente,RagioneSociale,CodiceCliente,RiferimentiInterni,Importo,Destinazione,Filiale,Magazzino,Preparato,Vettore,TipoTrasporto)\n" 
    + " VALUES(\n" 
    + "@NumeroDDT, @IdCantiere, @DataDDT, @Agente, @RagioneSociale, @CodiceCliente, @RiferimentiInterni, @Importo, @Destinazione, @Filiale, @Magazzino, @Preparato, @Vettore, @TipoTrasporto);\n" 
    + "SET IDENTITY_INSERT Bolla OFF;"; 


using (var conn = db.apriconnessione()) 
using (var cmd = new SqlCommand(sql, conn)) 
{ 
    //I have to guess at column types and lengths. Update these parameters to match your DB columns 
    cmd.Paramerers.Add("@NumeroDDT", SqlDbType.NVarChar, 50).Value = labelNUMDDTMOD.Text; 
    cmd.Paramerers.Add("@IdCantiere", SqlDbType.Int).Value = IdCantiere; 
    cmd.Paramerers.Add("@DataDDT", SqlDbType.DateTime).Value = Convert.ToDateTime(labelDATADDTMOD.Text); 
    cmd.Paramerers.Add("@Agente", SqlDbType.NVarChar, 50).Value = labelAgenteMOD.Text; 
    cmd.Paramerers.Add("@RagioneSociale", SqlDbType.NVarChar, 50).Value = labelRagioneSocialeMOD.Text; 
    cmd.Paramerers.Add("@CodiceCliente", SqlDbType.Int).Value = int.Parse(labelCodiceClienteMOD.Text); 
    cmd.Paramerers.Add("@RiferimentiInterni", SqlDbType.NVarChar, 50).Value = labelRIFInternoMOD.Text; 
    cmd.Paramerers.Add("@Importo", SqlDbType.Float).Value = float.Parse(labelImportoMOD.Text); //probably should be double or decimal 
    cmd.Paramerers.Add("@Destinazione", SqlDbType.NVarChar, 50).Value = labelDestMOd.Text; 
    cmd.Paramerers.Add("@Filiale", SqlDbType.NVarChar, 50).Value = labelFilialeMOD.Text; 
    cmd.Paramerers.Add("@Magazzino", SqlDbType.NVarChar, 50).Value = labelMagazzinoMOD.Text; 
    cmd.Paramerers.Add("@Preparato", SqlDbType.NVarChar, 50).Value = labelPreparatodaMOD.Text; 
    cmd.Paramerers.Add("@Vettore", SqlDbType.NVarChar, 50).Value = labelvettoreMOD.Text; 
    cmd.Paramerers.Add("@TipoTrasporto", SqlDbType.NVarChar, 50).Value = labelTipoTrasportoMOD.Text; 

    conn.Open(); 
    cmd.ExecuteNonQuery(); 
} 

注有在这里的一些其他方面的改进,太。我没有重写代码的其他部分,只是为了好玩。但是这个查询参数是重要的部分。