将datagridview数据插入sql数据库。 “解析查询时出错”

时间:2013-04-01 15:55:46

标签: c# sql database datagridview insert

EDITED:

我正在编写一个程序,它具有将excel文件加载到datagridview的功能。 Excel文件包含十列的产品列表。至于现在它工作正常。文件加载正常。但是在加载之后,程序应该使用来自datagridview的数据填充SQL Server CE数据库。因此,下次打开此表单时,datagridview应填充数据库中的数据。 (此excel加载功能是在我的公司更改内容时更新产品列表)。

我将此数据插入数据库时​​遇到问题。

我有一个错误:

  

解析查询时出错。 [令牌行号= 1,令牌行偏移= 67,令牌错误= Taq]

DNA是Excel文件单元格中的一个单词(第1行第3列(“ITEM”)。单元格的全部内容是AB-AB-0192 / A Taq DNA聚合酶(许可)。我认为问题是连接的在TAQ之前以某种方式显示空间。我测试了这个:当我删除那里的空格时,问题信息从Taq变为DNA。那么如何避免这种情况?excel文件中的所有列都设置为文本,而SQL Server CE数据库列是输入nvarchar

EDIT!

好的,你们让我走上了正确的道路:)。

这有效:

string strQuery = @"INSERT INTO TabelaProdukty VALUES (@VD, @ItemCode, @Item, @Qty, @Ppcur, @StandardPrice, @CeMarked, @Description, @Description2, @Edma)";
        sqlconnection.Open();
        using (System.Data.SqlServerCe.SqlCeCommand comm = new System.Data.SqlServerCe.SqlCeCommand(strQuery, sqlconnection))
        {
          comm.Parameters.Add("@VD", SqlDbType.NVarChar);
          comm.Parameters.Add("@ItemCode", SqlDbType.NVarChar);
          comm.Parameters.Add("@Item", SqlDbType.NVarChar);
          comm.Parameters.Add("@Qty", SqlDbType.NVarChar);
          comm.Parameters.Add("@Ppcur", SqlDbType.NVarChar);
          comm.Parameters.Add("@StandardPrice", SqlDbType.NVarChar);
          comm.Parameters.Add("@CeMarked", SqlDbType.NVarChar);
          comm.Parameters.Add("@Description", SqlDbType.NVarChar);
          comm.Parameters.Add("@Description2", SqlDbType.NVarChar);
          comm.Parameters.Add("@Edma", SqlDbType.NVarChar);

          for (int i = 0; i < dataGridView1.Rows.Count; i++)
          {

            comm.Parameters["@VD"].Value =  dataGridView1.Rows[i].Cells["VD"].Value;
            comm.Parameters["@ItemCode"].Value = dataGridView1.Rows[i].Cells["ItemCode"].Value;
            comm.Parameters["@Item"].Value = dataGridView1.Rows[i].Cells["ITEM"].Value;
            comm.Parameters["@Qty"].Value = dataGridView1.Rows[i].Cells["QUANTITY"].Value;
            comm.Parameters["@Ppcur"].Value = dataGridView1.Rows[i].Cells["PPCUR"].Value;
            comm.Parameters["@StandardPrice"].Value = dataGridView1.Rows[i].Cells["STANDARD_SELL_PRICE"].Value;
            comm.Parameters["@CeMarked"].Value = dataGridView1.Rows[i].Cells["CE-MARKED"].Value;
            comm.Parameters["@Description"].Value =  dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION"].Value;
            comm.Parameters["@Description2"].Value = dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION2"].Value;
            comm.Parameters["@Edma"].Value = dataGridView1.Rows[i].Cells["EDMA"].Value;


            comm.ExecuteNonQuery();

          }
          sqlconnection.Close();

数据库中填充了正确的数据,当我重新启动程序时,数据库已经填满。现在我只需要在添加新内容之前清除数据库。

但有一个问题。收到消息: 从一种数据类型转换为另一种数据类型时,数据被截断。 [功能名称(如果已知)=]

2 个答案:

答案 0 :(得分:1)

 SqlCeCommand cmd = new SqlCeCommand();
                cmd.CommandText =  "your insert statemnt (@param1.@param2,@param3) " ;


 cmd.Connection = this.sqlConnection1; //initialize your connection on page load 
                this.sqlConnection1.Open();

                  // add params 
                cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = your_control.Text; 
                cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = your_control.Text; 
                cmd.Parameters.Add("@param3", SqlDbType.VarChar).Value = your_control.Text; 

 cmd.ExecuteNonQuery();

this.sqlConnection1.Close();

您只需在声明它们之后将数值从数据集中提取出来即可。

答案 1 :(得分:1)

尝试这样的事情 - 在ADO.NET查询中使用参数!并将SqlCeConnectionSqlCeCommand等所有一次性物品放入using(...) { .... }块,以确保它们妥善处置:

private void button1_Click(object sender, EventArgs e) // wczytanie excela 
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    var dialogResult = openFileDialog1.ShowDialog();
    string sWybranyPlik;

    if (dialogResult  == DialogResult.OK)
    {
      sWybranyPlik = openFileDialog1.FileName;

      try
      {
         using(OleDbConnection ExcelConnection = new OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + sWybranyPlik + "';Extended Properties=Excel 8.0;"))
         {
         OleDbDataAdapter OleDBAdapter = new OleDbDataAdapter("select * from [Tabelle1$]", ExcelConnection);

         OleDBAdapter.Fill(DtSet.Tables[0]);
         dataGridView1.DataSource = DtSet.Tables[0];

             -- recommendation: always explicitly *specify* the columns of the table
             -- that you're inserting into
         string strQuery = @"INSERT INTO TabelaProdukty(col1, col2, col3,....., colN)
                     VALUES (@VD, @ItemCode, @Item, @Qty, @Ppcur, @StandardPrice, @CeMarked, @Description, @Description2, @Edma)";

                 using(sqlconnection = new SqlCeConnection("Data Source = C:\\Users\\user\\Documents\\Visual Studio 2010\\Projects\\BMGRP\\Oferty BMGRP\\Oferty BMGRP\\bin\\Debug\\BazaDanych.sdf"))
         using(SqlCeCommand comm = new SqlCeCommand(strQuery, sqlconnection))
         {
             comm.Parameters.AddWithValue("@VD", dataGridView1.Rows[i].Cells["VD"].Value);
             comm.Parameters.AddWithValue("@ItemCode", dataGridView1.Rows[i].Cells["ItemCode"].Value);
             comm.Parameters.AddWithValue("@Item", dataGridView1.Rows[i].Cells["ITEM"].Value);
             comm.Parameters.AddWithValue("@Qty", dataGridView1.Rows[i].Cells["QUANTITY"].Value);
             comm.Parameters.AddWithValue("@Ppcur", dataGridView1.Rows[i].Cells["PPCUR"].Value);
             comm.Parameters.AddWithValue("@StandardPrice", dataGridView1.Rows[i].Cells["STANDARD_SELL_PRICE"].Value);
             comm.Parameters.AddWithValue("@CeMarked", dataGridView1.Rows[i].Cells["CE-MARKED"].Value);
             comm.Parameters.AddWithValue("@Description", dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION"].Value);
             comm.Parameters.AddWithValue("@Description2", dataGridView1.Rows[i].Cells["ITEM_DESCRIPTION2"].Value);
             comm.Parameters.AddWithValue("@Edma", dataGridView1.Rows[i].Cells["EDMA"].Value);

             sqlconnection.Open();
             comm.ExecuteNonQuery();
             sqlconnection.Close();
         }

         ExcelConnection.Close();
          }      
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
    }