ORA-01036:更新gridview时非法变量名称/编号

时间:2015-10-01 13:01:09

标签: c# asp.net oracle gridview

我想更新gridview中行的单元格。但是我收到了错误

  

ORA-01036:非法变量名称/编号

cmd.ExecuteNonQuery()

以下是我的代码: -

protected void SaveChanges(object sender, EventArgs e)
{   
    myConn.Open();
    string excelData = Grid1ExcelData.Value;
    string excelDeletedIds = Grid1ExcelDeletedIds.Value;

    string[] rowSeparator = new string[] { "|*row*|" };
    string[] cellSeparator = new string[] { "|*cell*|" };

    string[] dataRows = excelData.Split(rowSeparator, StringSplitOptions.None);

    for (int i = 0; i < dataRows.Length; i++)
    {
        string[] dataCells = dataRows[i].Split(cellSeparator, StringSplitOptions.None);
        string mkey = dataCells[0];
        string shipName = dataCells[1];
        string shipCity = dataCells[2];
        string shipAddress = dataCells[3];
        string shipCountry = dataCells[4];
        string orderDate = dataCells[5];
        bool sent = dataCells[6] == "yes";

        string insertUpdateQuery = "";
        if (!string.IsNullOrEmpty(mkey))
        {
            insertUpdateQuery = "UPDATE B_Order_new SET ShipName = @ShipName, ShipCity = @ShipCity, ShipAddress = @ShipAddress, ShipCountry = @ShipCountry, OrderDate = @OrderDate, Sent = @Sent WHERE MKEY = @MKEY";
        }
        else
        {
            insertUpdateQuery = "INSERT INTO B_Order_new (ShipName, ShipCity, ShipAddress, ShipCountry, OrderDate, Sent) " +
                                 "VALUES(@ShipName, @ShipCity, @ShipAddress, @ShipCountry, @OrderDate, @Sent)";
        }
        OracleCommand cmd = new OracleCommand(insertUpdateQuery, myConn);
        var orderedOn = DateTime.ParseExact(orderDate, "dd/MM/yyyy", null);
        cmd.Parameters.Add("@ShipName", OracleType.VarChar).Value = shipName;
        cmd.Parameters.Add("@ShipCity", OracleType.VarChar).Value = shipCity;
        cmd.Parameters.Add("@ShipAddress", OracleType.VarChar).Value = shipAddress;
        cmd.Parameters.Add("@ShipCountry", OracleType.VarChar).Value = shipCountry;
        cmd.Parameters.Add("@OrderDate", OracleType.DateTime).Value = orderedOn;
        cmd.Parameters.Add("@Sent", OracleType.Char).Value = true;

        if (!string.IsNullOrEmpty(mkey))
        {
            cmd.Parameters.Add("@MKEY", OracleType.Number).Value = mkey;
        }
        cmd.ExecuteNonQuery();
        if (insertUpdateQuery != "")
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "CloseScript", "alert('Data Updated succesfully');", true);
        }
    }
    if (!string.IsNullOrEmpty(excelDeletedIds))
    {
        OracleCommand deleteComm = new OracleCommand("DELETE FROM Orders WHERE OrderID IN (" + excelDeletedIds + ")", myConn);
        deleteComm.ExecuteNonQuery();
    }
    myConn.Close();
    Grid1.DataBind();
}

1 个答案:

答案 0 :(得分:2)

问题是你使用了错误的绑定变量语法(看起来你正在使用SQL Server参数绑定语法)。 Oracle的ADO.NET提供商希望您使用:前缀而不是@

所以尝试这样的事情:

insertUpdateQuery = "UPDATE B_Order_new SET ShipName = :ShipName, ShipCity = :ShipCity, ...

然后在设置值时,您不需要为它添加前缀:

cmd.Parameters.Add("ShipName", OracleType.VarChar).Value = shipName;
cmd.Parameters.Add("ShipCity", OracleType.VarChar).Value = shipCity;
...

修改

此外,如果您按名称绑定参数,请确保将OracleCommand.BindByName属性设置为true。否则,绑定将在位置进行。