1

我正在使用 ODP.net 使用 C# winform 更新表。如果我的 oracle 表约束没有验证,我会收到动态 SQL 错误。如果我没有删除/编辑工作正常。

请告诉除以下选项之外如何解决此问题 1. 在代码中手动设置主键,因为代码是通用的。2.删除oracle表中约束内的novalidate。


Oracle 表约束:

ALTER TABLE "table1" ADD CONSTRAINT "PK_table1" PRIMARY KEY ("EMP_ID") ENABLE NOVALIDATE;

C#代码:

using Oracle.DataAccess.Client;

public partial class Form1 : Form
    {
        OracleConnection OraConn;
        OracleDataAdapter FOraDataAdapt;
        DataTable OraDataTable;
        BindingSource FBindSource;
        int RecNo;

        public Form1()
        {
            InitializeComponent();
            OraConn = new OracleConnection("Data Source=ORCL1;User Id=123;Password=123;");
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                OraConn.Open();

                OracleCommand FOraCommand = new OracleCommand("select * from table1", OraConn);
                FOraDataAdapt = new OracleDataAdapter(FOraCommand);                   OracleCommandBuilder FOraCommandBuilder = new OracleCommandBuilder(FOraDataAdapt);
                OraDataTable = new DataTable();

                DataTable TableSchema = new DataTable();
                FOraDataAdapt.Fill(OraDataTable);
                FOraDataAdapt.FillSchema(TableSchema, System.Data.SchemaType.Source);

                FBindSource = new BindingSource();
                FBindSource.DataSource = OraDataTable;

                grdView.DataSource = FBindSource; 

            }
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                RecNo = FBindSource.Position;
                FBindSource.RemoveAt(RecNo);
                FOraDataAdapt.Update(OraDataTable);            
                OraDataTable.Rows[RecNo].AcceptChanges();
            }
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            RecNo = FBindSource.Position;
            OraDataTable.Rows[RecNo].BeginEdit();
        }

        private void btnPost_Click(object sender, EventArgs e)
        {
            try
            {
                FBindSource.EndEdit();
                FOraDataAdapt.Update(OraDataTable);
                OraDataTable.Rows[RecNo].AcceptChanges();
            }
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
        }
    }
4

0 回答 0