我使用ODP.net用C#winform更新表。如果我的oracle表约束没有验证,我会收到动态SQL错误。如果我没有删除/编辑工作正常。
除了以下选项外,请告诉我们如何解决此问题 1.由于代码是通用的,因此在代码中手动设置primarykey。 2.删除oracle表中constarint中的novalidate。
Oracle表constarint:
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);
}
}
}