试图插入值获取错误:并非所有变量都绑定

时间:2017-11-27 05:36:33

标签: c# jquery asp.net oracle ado.net

我正在尝试使用insert命令在表中插入值,如下面的代码所示,但是得到此错误“并非所有变量都绑定”。我尝试了很多,但它是混淆的。问题发生在cmd.executenonquery.Any帮助将不胜感激。

c#c​​ode

[WebMethod]
 public static void SaveUser(User user)
    {
 String connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
        using (OracleConnection con = new OracleConnection(connectionString))
        {
            using (OracleCommand cmd = new OracleCommand("INSERT INTO par_cinfo(Product_Id,CUSTOMER_NAME,CITY,CDATE) VALUES (:Product_Id,:CUSTOMER_NAME,:CITY,:CDATE)", con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("CUSTOMER_NAME", user.CUSTOMER_NAME);
                cmd.Parameters.AddWithValue("CITY", user.CITY);
                cmd.Parameters.AddWithValue("CDATE", user.CDATE);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();               
                con.Close();
            }
  }
    }

}
public class User
{
    public decimal Product_Id { get; set; }
    public string CUSTOMER_NAME { get; set; }
    public string CITY { get; set; }
    public DateTime CDATE { get; set; }
}

单击按钮上的Jquery

<script type="text/javascript">
        $(document).ready(function () {
            var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
            var date = new Date();
            var val = date.getDate() + "-" + monthNames[date.getMonth()] + "-" + date.getFullYear();
            $("#Label2").text(val);
        });



 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=Button3]").bind("click", function () {
            var user = {};
            user.Product_Id = 1;
            user.CUSTOMER_NAME = $("[id*=TextBox2]").val();
            user.City = $("[id*=TextBox3]").val();
            user.cdate = $("[id*=Label2]").text();
            $.ajax({
                type: "POST",
                url: "Default.aspx/SaveUser",
                data: '{user: ' + JSON.stringify(user) + '}',
                //data: JSON.stringify({user:user}),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert("User has been added successfully.");
                    window.location.reload();
                }
            });
            return false;
        });
    });
</script>
            </script>

我在oracle中创建的表格仅用于输入表格中的数据

Create table par_cinfo
     (
      Product_Id         NUMBER(10) NOT NULL PRIMARY KEY,
      CUSTOMER_NAME      VARCHAR2(10 BYTE) NOT NULL,
      CITY               VARCHAR2(10 BYTE) NOT NULL,
      CDATE              DATE

      ) 

2 个答案:

答案 0 :(得分:1)

首先,当您在SQL命令中声明变量时,请使用@,而不是:

using (OracleCommand cmd = new OracleCommand("INSERT INTO par_cinfo(Product_Id,CUSTOMER_NAME,CITY,CDATE) VALUES (@Product_Id,@CUSTOMER_NAME,@CITY,@CDATE)", con))

其次,在您的SQL INSERT命令中,您声明了一个名为Product_Id的参数,但您从未将其添加到cmd.Parameters。所以你需要添加:

cmd.Parameters.AddWithValue("Product_Id", user.Product_Id);

答案 1 :(得分:0)

Product_Id提供参数。如果是标识列,则从命令中删除字段名称。

using (OracleCommand cmd = new OracleCommand("INSERT INTO par_cinfo(CUSTOMER_NAME,CITY,CDATE) VALUES (@CUSTOMER_NAME,@CITY,@CDATE)", con)