我应该使用什么查询语法在Oracle数据库中插入记录?

时间:2015-04-22 11:26:36

标签: c# oracle sql-insert query-parameters

我是初学者并尝试在C#中创建一个简单的程序,用于在Oracle数据库中插入和更新记录。我已成功连接到数据库,但我的SQL语句异常表明不支持(?)符号。为什么我会收到此异常,如何解决此问题?

我的代码是:

private void btnSave_Click(object sender, EventArgs e)
{
    OracleConnection con = null;
    try
    {
        con = new OracleConnection();
        string constr = "Data source=XE; User ID=cloudester; Password=cloudester123;";
        if (con.State != ConnectionState.Open)
        {
            try
            {
                con.ConnectionString = constr;
                con.Open();
                //MessageBox.Show("Successfull connection");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception caught");
            }
        }

        if (con.State == ConnectionState.Open)
        {
            string str = "Insert into EMP_DETAIL(EmpId, Name, Age)";
            str += "values (?,?,?)";

            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = Text;
            cmd.Connection = con;
            cmd.Parameters.Add(new OracleParameter("EmpId", OracleDbType.Varchar2)).Value = txtEmpId;
            cmd.Parameters.Add(new OracleParameter("Name", OracleDbType.Varchar2)).Value = txtName;
            cmd.Parameters.Add(new OracleParameter("Age", OracleDbType.Int16)).Value = int.Parse(txtAge.Text);

            cmd.ExecuteNonQuery();
        }
    }
    catch { ... }
}

0 个答案:

没有答案