3

我可以先说这是我 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);
        }
4

1 回答 1

2

多么值得学习的一课。显然,必须将 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();
        }
于 2012-09-19T22:07:58.773 回答