gpt4 book ai didi

c# - Oracle 插入期间 ASP.NET C# 缺少表达式错误

转载 作者:行者123 更新时间:2023-11-30 17:09:50 25 4
gpt4 key购买 nike

首先我可以说这是我 10 年来第一个分配的数据库项目......也是第一次用 C# 完成它。我“只是”尝试使用 OleDB 将表单数据插入到 Oracle 表中。

我不断收到“ORA-00936:缺少表达式”。下面是我的代码...知道缺少什么吗?

public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}

private void executeInsert(string EventType, string EventSubType, string DeptName, string EventDate, string Duration, string EventName, string EventAdd, string WardNo, string Program, string NumAtt, string StTime, string EndTime, string MngName, string RecKeeper)
{
OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) VALUES "
+ "(@EventType, @EventSubType, @DeptName, TO_DATE(@EventDate, 'Month dd, YYYY'), @Duration, @EventName, @EventAdd, @WardNo, @Program, @NumAtt, TO_DATE(@StTime, 'HH:MI:SS PM'), TO_DATE(@EndTime, 'HH:MI:SS PM'), @MngName, @RecKeeper)";

try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbParameter[] param = new OleDbParameter[14];
param[0] = new OleDbParameter("@EventType", OleDbType.VarChar, 25);
param[1] = new OleDbParameter("@EventSubType", OleDbType.VarChar, 80);
param[2] = new OleDbParameter("@DeptName", OleDbType.VarChar, 240);
param[3] = new OleDbParameter("@EventDate", OleDbType.Date);
param[4] = new OleDbParameter("@Duration", OleDbType.Numeric);
param[5] = new OleDbParameter("@EventName", OleDbType.VarChar, 80);
param[6] = new OleDbParameter("@EventAdd", OleDbType.VarChar, 150);
param[7] = new OleDbParameter("@WardNo", OleDbType.VarChar, 25);
param[8] = new OleDbParameter("@Program", OleDbType.VarChar, 150);
param[9] = new OleDbParameter("@NumAtt", OleDbType.Numeric);
param[10] = new OleDbParameter("@StTime", OleDbType.Date);
param[11] = new OleDbParameter("@EndTime", OleDbType.Date);
param[12] = new OleDbParameter("@MngName", OleDbType.VarChar, 150);
param[13] = new OleDbParameter("@RecKeeper", OleDbType.VarChar, 150);

param[0].Value = EventType;
param[1].Value = EventSubType;
param[2].Value = DeptName;
param[3].Value = EventDate;
param[4].Value = Duration;
param[5].Value = EventName;
param[6].Value = EventAdd;
param[7].Value = WardNo;
param[8].Value = Program;
param[9].Value = NumAtt;
param[10].Value = StTime;
param[11].Value = EndTime;
param[12].Value = MngName;
param[13].Value = RecKeeper;

for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}

cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}

catch (Exception ex) { throw ex; }

finally
{
conn.Close();
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);

TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();

executeInsert(rbEventType.SelectedItem.Text, ddVolType.SelectedItem.Text,
txtDept.Text, txtEventDate.Text, meetDuration, txtEventName.Text,
txtEventAdd.Text, ddWard.SelectedItem.Value, txtSBPlan.Text, txtNumVol.Text,
txtStTime.Text, txtEndTime.Text, txtEventMgr.Text, txtRecording.Text);
}

更新代码:

public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}

private void executeInsert(string EventType, string EventSubType, string DeptName, string EventDate, string Duration, string EventName, string EventAdd, string WardNo, string Program, string NumAtt, string StTime, string EndTime, string MngName, string RecKeeper)
{

OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) VALUES "
+ "(@EventType, @EventSubType, @DeptName, TO_DATE(@EventDate, 'Month dd, YYYY'), @Duration, @EventName, @EventAdd, @WardNo, @Program, @NumAtt, TO_DATE(@StTime, 'HH:MI:SS PM'), TO_DATE(@EndTime, 'HH:MI:SS PM'), @MngName, @RecKeeper)";

try
{
conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;

cmd.Parameters.AddRange(new OleDbParameter[]
{
new OleDbParameter("@EventType", EventType),
new OleDbParameter("@EventSubType", EventSubType),
new OleDbParameter("@DeptName", DeptName),
new OleDbParameter("@EventDate", EventDate),
new OleDbParameter("@Duration", Duration),
new OleDbParameter("@EventName", EventName),
new OleDbParameter("@EventAdd", EventAdd),
new OleDbParameter("@WardNo", WardNo),
new OleDbParameter("@Program", Program),
new OleDbParameter("@NumAtt", NumAtt),
new OleDbParameter("@StTime", StTime),
new OleDbParameter("@EndTime", EndTime),
new OleDbParameter("@MngName", MngName),
new OleDbParameter("@RecKeeper", RecKeeper)
});

cmd.ExecuteNonQuery();
}
}

catch (Exception ex) { throw ex; }

finally
{
conn.Close();
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{

var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);

TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();

executeInsert(rbEventType.SelectedItem.Text, ddVolType.SelectedItem.Text,
txtDept.Text, txtEventDate.Text, meetDuration, txtEventName.Text,
txtEventAdd.Text, ddWard.SelectedItem.Value, txtSBPlan.Text, txtNumVol.Text,
txtStTime.Text, txtEndTime.Text, txtEventMgr.Text, txtRecording.Text);
}

最佳答案

多么值得学习的一课。显然,ORACLE 参数必须显示为“?”

所以,我的最终代码......实际成功插入记录的是:

public string getConnString()
{
//set the connection string from web config file
return WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}

private void executeInsert()
{
OleDbConnection conn = new OleDbConnection(getConnString());
string sql = "INSERT INTO APPS.CLV_EVENT_TRACK (EVENTTYPE, EVENTSUBTYPE, DEPTNAME, EVENTDATE, DURATION, EVENTNAME, EVENTADD, WARDNO, PROGRAM, NUMATT, STARTTIME, ENDTIME, MNGNAME, RECORDKEEPER) ";
sql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

try
{
var start = DateTime.Parse(txtStTime.Text);
var end = DateTime.Parse(txtEndTime.Text);

TimeSpan duration = end.Subtract(start);
string meetDuration = duration.TotalMinutes.ToString();

conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = rbEventType.SelectedItem.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = ddVolType.SelectedItem.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtDept.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtEventDate.Text;
cmd.Parameters.Add("?", OleDbType.Numeric).Value = meetDuration;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventName.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventAdd.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = ddWard.SelectedItem.Value;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtSBPlan.Text;
cmd.Parameters.Add("?", OleDbType.Numeric).Value = txtNumVol.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtStTime.Text;
cmd.Parameters.Add("?", OleDbType.Date).Value = txtEndTime.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtEventMgr.Text;
cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtRecording.Text;

cmd.ExecuteNonQuery();
}
}

catch (Exception ex) { throw ex; }

finally
{
conn.Close();
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
executeInsert();
}

关于c# - Oracle 插入期间 ASP.NET C# 缺少表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com