从C#中的datagrid视图插入数据库中的错误

时间:2013-05-11 10:42:05

标签: c# .net datagridview

我有一个小问题,不知道它是什么

我正在尝试以下查询将数据从我的datagridview插入到我的数据库中 问题是当我执行查询时它会给我以下错误

  

','附近的语法不正确。

但是数据已成功插入数据库中。我试图在这里找到答案,但无济于事。有人请指出错误。

我的代码:

private void button2_Click(object sender, EventArgs e)
{
   SqlConnection conn = new SqlConnection(@"Data Source HERE");
   conn.Open();

   int rn = 0;

   while (rn < dgvmain.Rows.Count)
   {
       SqlCommand cmd = new SqlCommand("Insert into mainTab(Token_number,ItemType,Quantity,Amount) values(" + dgvmain.Rows[rn].Cells[0].Value + ",'" + dgvmain.Rows[rn].Cells[1].Value + "'," + dgvmain.Rows[rn].Cells[2].Value + "," + dgvmain.Rows[rn].Cells[3].Value + ")", conn);

       cmd.ExecuteNonQuery();
       rn++;
   }
}

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

private void button2_Click(object sender, EventArgs e)
{
        // put your SqlConnection into a using block
        using (SqlConnection conn = new SqlConnection(@"Data Source HERE"))
        {
            // define query with parameters
            string queryStmt = "INSERT INTO dbo.mainTab(Token_number, ItemType, Quantity, Amount) " +
                               "VALUES(@TokenNumber, @ItemType, @Quantity, @Amount)";

            // put your SqlCommand into a using block
            using (SqlCommand cmd = new SqlCommand(queryStmt, conn))
            {
                // Add parameter definitions to SqlCommand
                cmd.Parameters.Add("@TokenNumber", SqlDbType.Int);
                cmd.Parameters.Add("@ItemType", SqlDbType.Int);
                cmd.Parameters.Add("@Quantity", SqlDbType.Decimal);
                cmd.Parameters.Add("@Amount", SqlDbType.Decimal);

                int rn = 0;

                // now open - as late as possible!
                conn.Open();

                // iterate
                while (rn < dgvmain.Rows.Count)
                {
                    // set parameter values
                    cmd.Parameters["@TokenNumber"].Value = Convert.ToInt32(dgvmain.Rows[rn].Cells[0].Value);
                    cmd.Parameters["@ItemType"].Value = Convert.ToInt32(dgvmain.Rows[rn].Cells[1].Value);
                    cmd.Parameters["@Quantity"].Value = Convert.ToDecimal(dgvmain.Rows[rn].Cells[2].Value);
                    cmd.Parameters["@Amount"].Value = Convert.ToDecimal(dgvmain.Rows[rn].Cells[3].Value);

                    // execute INSERT statement
                    cmd.ExecuteNonQuery();

                    rn++;
                }

                conn.Close();
            }
        }
}

答案 1 :(得分:1)

这是错的。请改变它:

       while (rn < dgvmain.Rows.Count)
       {
           SqlCommand cmd = new SqlCommand("Insert into mainTab(Token_number,ItemType,Quantity,Amount) values(@tok,@itm,@qua,@amo)", conn);
           cmd.Parameters.AddWithValue("@tok", dgvmain.Rows[rn].Cells[0].Value);
           cmd.Parameters.AddWithValue("@itm", dgvmain.Rows[rn].Cells[1].Value);
           cmd.Parameters.AddWithValue("@qua", dgvmain.Rows[rn].Cells[2].Value);
           cmd.Parameters.AddWithValue("@amo", dgvmain.Rows[rn].Cells[3].Value);
           cmd.ExecuteNonQuery();
           rn++;
       }