使用C#WPF添加数据



我的目标是将数据添加到Oracle数据库中。我已经在这个链接上尝试了教程(https://youtu.be/LUKp76CNmJY)这对初学者很有帮助。但是INSERT不起作用(DELETE也不起作用(。但是UPDATE是有效的。使用典型的人力资源账户。是否为app.config指定某种排序特权?我不知道。(UPDATE有效,这意味着连接成功,更新被授予HR。(

代码的一部分如下。

app.config

...
<connectionStrings>
<add name="myConnectionString" connectionString="DATA SOURCE=ORCL;PASSWORD=Oracle11;USER ID=HR"
providerName="Oracle.ManagedDataAccess.Client" />
</connectionStrings>
...

CS文件

...
private void updateDataGrid()  //SELECT statement isuued.
{
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT EMPLOYEE_ID,LAST_NAME,JOB_ID,HIRE_DATE,EMAIL FROM EMPLOYEES";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
myDataGrid.ItemsSource = dt.DefaultView;
dr.Close();
}
private void setConnection() //Getting connection.
{
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
con = new OracleConnection(connectionString);
try
{ con.Open(); } catch (Exception exp) { }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{   this.updateDataGrid();  }
...

private void add_btn_Click(object sender, RoutedEventArgs e)  //Event listner. 
{
string sql = "INSERT INTO EMPLOYEES(EMPLOYEE_ID,LAST_NAME,EMAIL,HIRE_DATE,JOB_ID)" +
"VALUES(:EMPLOYEE_ID,:LAST_NAME,:EMAIL,:HIRE_DATE,:JOB_ID)";
this.AUD(sql, 0);
...
}

...
private void AUD(string sql_stmt, int state)  // Making Oracle statement string
{
string msg = "";
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = sql_stmt;
cmd.CommandType = CommandType.Text;
switch (state)
{
case 0:
msg = "Row Inserted Successfully!";
cmd.Parameters.Add("EMPLOYEE_ID", OracleDbType.Int32, 6).Value = Int32.Parse(employee_id_txtbx.Text);
cmd.Parameters.Add("LAST_NAME", OracleDbType.Varchar2, 25).Value = last_name_txtbx.Text;
cmd.Parameters.Add("EMAIL", OracleDbType.Varchar2, 25).Value = email_txtbx.Text;
cmd.Parameters.Add("HIRE_DATE", OracleDbType.Date, 7).Value = hire_date_picker.SelectedDate;
cmd.Parameters.Add("JOB_ID", OracleDbType.Varchar2, 10).Value = job_id_txtbx.Text;
break;
...
}
try
{
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show(msg);
this.updateDataGrid();
}
}
catch (Exception expe) { };
}
...

我没有得到HR的架构。JOB_ID列是FK。因此,当在JOB_ID处输入到文本框时,JOB_ID不应该任意输入。例如,插入员工(EMPLOYEE_ID、LAST_NAME、电子邮件、HIRE_DATE、JOB_ID(值(90,"ss"、"ss","2020-10-09"、"ssss"(;--不!插入员工(EMPLOYEE_ID、LAST_NAME、电子邮件、HIRE_DATE、JOB_ID(值(90,"ss"、"sss"、"2020-10-09"、"AD_PRES"(--OK

相关内容

  • 没有找到相关文章

最新更新