这是 sql 的 c# 编码。值更改时出现语法错误

This is a c# coding with sql. Syntax error when value changes

我是 c# 和 sql 的新手。当我插入联系人号码时,我的代码显示语法错误。有 10 位数字,但小于它时不会出错。 table 中的数据类型也是 big int. 我正在使用 sql 服务器 2008。请帮助。 代码如下。

            sQuery = "insert into ReceiveChallanPrint1 (ChallanNO,ReferencNo,Title,Author,ISBN,ChallanDate," +
                "ReceivedOn,Publisher,PublisherAddress,PublisherContactNo,PublisherEmail,TemplateName," +
                "TemplateSubject,TemplateBody,EmailAttachment1"
                + ")";

        sQuery = sQuery + " values(";

        sQuery = sQuery + "'" + Convert.ToInt32( dgvChallanActive.Rows[i].Cells["ChallanNo"].Value.ToString()) + "',";
        sQuery = sQuery + "'"+Convert.ToInt32( dgvChallanActive.Rows[i].Cells["ReferenceNo"].Value.ToString())+ "',";
        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["Title"].Value) + "',";
        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["Author1"].Value) + "',";
        sQuery = sQuery + "'" + dgvChallanActive.Rows[i].Cells["ISBN"].Value + "',";


        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["ChallanDate"].Value) + "',";
        sQuery = sQuery + "'" + dtpReceivedDate.Value.ToShortDateString() + "',";

        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["Publisher"].Value) + "',";
        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["PublisherAddress"].Value) + "',";


       // sQuery = sQuery + "'" + Convert.ToInt32(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToString()) + "',";

        sQuery = sQuery + "'" + Convert.ToInt32(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToString()) + "',";

        sQuery = sQuery + "'" + Convert.ToString(dgvChallanActive.Rows[i].Cells["PublisherEmail"].Value) + "',";
        sQuery = sQuery + "'" + Convert.ToString(cboEmailTemplate.Text) + "',";
        sQuery = sQuery + "'" + sSubject + "',";
        sQuery = sQuery + "'" +sBody+ "',";
        sQuery = sQuery + "'" + lblAttachment.Text.Trim() + "'";
        sQuery = sQuery + ")";


        DBInteraction.DBOperation.sConnectionString = GlobalFuncs.sConnectionString;
        sRetVal = DBOperation.ExecuteDBOperation(sQuery, DBOperation.OperationType.INSERT, null, ref dt);

         if (sRetVal != GlobalFuncs.SUCCESS)
        { MessageBox.Show("no insert "); }

        return sRetVal;
}

    catch (Exception ex)
    {
        return ex.Message;
    }

因为我在这段代码中使用了调试选项,所以在穿越 PublisherContactNo 之后。它来自尝试 box.I 我正在从 gridview 中获取数据。

Convert.ToInt32(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToSt‌​ring())更改为Convert.ToInt64(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToSt‌​ring())

如果您更改

,这应该可以正常工作
Convert.ToInt32(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToString()) 

Convert.ToInt64(dgvChallanActive.Rows[i].Cells["PublisherContactNo"].Value.ToString()) 

但这根本不是写代码的好习惯。您需要使用参数化查询,例如:

string query = "insert into tbl1(col1,col2,col3) values(@col1,@col2,@col3)";

然后你需要传递参数值。