0

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

我正在尝试以下查询将数据从我的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++;
   }
}
4

2 回答 2

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();
            }
        }
}
于 2013-05-11T10:54:59.497 回答
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++;
       }
于 2013-05-11T10:46:21.860 回答