我试图从asp .net中插入oracle表中的值,但是遇到问题"非法变量名称/号码"问题
cmd.ExecuteNonQuery();
虽然传递给SaveFrmDetails的数据如下所示。任何帮助将不胜感激。
user = {Product_Id: "1", Tdcno: "tw2", Revision: "0", Revision_Date: "23-Nov-2017", P_Group: "Chain Link",Prod_Desc: "descr",N_I_Prd_Std:"india" ,Appln:"appllll",Frm_Supp:"Frmm",Created_Date: "23-nov-2017",Created_By:"Mohan"}
class property
public class User
{
public decimal Product_Id { get; set; }
public string Tdcno { get; set; }
public decimal Revision { get; set; }
public DateTime Revision_Date { get; set; }
public string P_Group { get; set; }
public string Prod_Desc { get; set; }
public string N_I_Prd_Std { get; set; }
public string Appln { get; set; }
public string Frm_Supp { get; set; }
public DateTime Created_Date { get; set; }
public string Created_By { get; set; }
}
和我通过ajax调用在用户对象中发送的日期部分就像这样
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();
$("#Revision_Date").text(val);
使用用户对象
通过ajax在SaveFrmDetails webmethod中传递的值user.Revision_Date = $("[id*=Revision_Date]").text();
c#code
public static void SaveFrmDetails(User user)
{
string connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
using (OracleConnection con = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand("INSERT INTO TDC_PRODUCT1(PRODUCT_ID,TDC_NO, REVISION,REVISION_DATE,P_GROUP,PROD_DESC,N_I_PRD_STD,APPLN,FRM_SUPP,CREATED_DATE,CREATED_BY) VALUES (:Product_Id,:Tdc_No,:Revision,:Revision_Date,:P_Group,:Prod_Desc,:N_I_Prd_Std,:Appln,:Frm_Supp,:Created_Date,:Created_By)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Product_Id", user.Product_Id);
cmd.Parameters.AddWithValue("Tdc_No", user.Tdcno);
cmd.Parameters.AddWithValue("Revision", user.Revision);
cmd.Parameters.AddWithValue("Revision_Date", user.Revision_Date);
cmd.Parameters.AddWithValue("P_Group", user.P_Group);
cmd.Parameters.AddWithValue("Prod_Desc", user.Prod_Desc);
cmd.Parameters.AddWithValue("N_I_Prd_Std", user.N_I_Prd_Std);
cmd.Parameters.AddWithValue("Appln", user.Appln);
cmd.Parameters.AddWithValue("Frm_Supp", user.Frm_Supp);
cmd.Parameters.AddWithValue("Created_Date", user.Created_By);
cmd.Parameters.AddWithValue("Created_By", user.Created_By);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
oracle表设计
create table TDC_PRODUCT1
(
PRODUCT_ID NUMBER(10) not null,
TDC_NO VARCHAR2(10) not null,
REVISION NUMBER(10) not null,
REVISION_DATE DATE,
P_GROUP VARCHAR2(100) not null,
PROD_DESC VARCHAR2(100) not null,
N_I_PRD_STD VARCHAR2(100) not null,
APPLN VARCHAR2(100) not null,
FRM_SUPP VARCHAR2(100) not null,
CREATED_DATE DATE,
CREATED_BY VARCHAR2(30) not null
)
答案 0 :(得分:0)
首先,您应该使用占位符。解析时
INSERT INTO TDC_PRODUCT1 VALUES(Product_Id,Tdc_No, Revision,Revision_Date,P_Group,Prod_Desc,N_I_Prd_Std,Appln,Frm_Supp,Created_Date,Created_By)
Oracle找不到任何放置参数值的地方。请改用:Product_Id
,:Tdc_No
等等。
接下来,使用没有列列表的INSERT是不好的做法。这意味着每次在表中添加新列时都会破坏您的应用程序。
答案 1 :(得分:0)
更改
INSERT INTO TDC_PRODUCT1 VALUES(Product_Id,Tdc_No, Revision,Revision_Date,P_Group,Prod_Desc,N_I_Prd_Std,Appln,Frm_Supp,Created_Date,Created_By)
要
INSERT INTO TDC_PRODUCT1 (Product_Id,Tdc_No, Revision,Revision_Date,P_Group,Prod_Desc,N_I_Prd_Std,Appln,Frm_Supp,Created_Date,Created_By)
VALUES (:Product_Id,:Tdc_No,:Revision,:Revision_Date,:P_Group,:Prod_Desc,:N_I_Prd_Std,:Appln,:Frm_Supp,:Created_Date,:Created_By)
您(可选)在VALUES
关键字之前指定列名称,但如果您使用绑定变量,则需要使用冒号:
为这些变量添加前缀以指示此情况。