如何将C#中的文本框值插入Oracle数据库

时间:2017-03-30 00:17:28

标签: c# sql oracle winforms

好的,我是所有这一切的新手,并且正在努力在Windows窗体应用程序中插入TextBox值(用户输入)并将这些值存储到Oracle数据库表中。

C#代码:

string oradb = "DATA SOURCE = larry.uopnet.plymouth.ac.uk:1521/orcl.fost.plymouth.ac.uk;PERSIST SECURITY INFO = True;USER ID = xxxxxxxxxx;password = xxxxxxxxx";

OracleConnection con = new OracleConnection(oradb);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "Insert into USER.Client VALUES (txtBoxClientName, txtBoxClientCity, txtBoxClientCountry, txtBoxClientNumber, txtBoxClientURL, comboClientStatus)";

int rowsUpdated = cmd.ExecuteNonQuery();
if (rowsUpdated == 0)
    MessageBox.Show("Please fill in required fields");
else
    MessageBox.Show("Client has been added");
con.Dispose();

此错误出现在int rowsUpdated = cmd.ExecuteNonQuery();行 - Oracle.DataAccess.dll中出现未处理的Oracle.DataAccess.Client.OracleException类型的异常

Oracle中的触发器代码:

create or replace TRIGGER trg_client_ClientID
BEFORE INSERT ON client FOR EACH ROW
BEGIN
  :NEW.ClientID := seq_client_ClientID.nextval;
END;

2 个答案:

答案 0 :(得分:3)

我已经设法通过添加OracleException和ArgumentException并使用来自Elkhan的参数代码来修复它 - 这是我修改过的代码:

    private void btnClientSave_Click(object sender, EventArgs e)
    {

        string oradb = "DATA SOURCE = larry.uopnet.plymouth.ac.uk:1521/orcl.fost.plymouth.ac.uk;PERSIST SECURITY INFO = True;USER ID = xxxxxxxxx;password = xxxxxxxx";
        string insertquery = "Insert into Client VALUES (:1, :2, :3, :4, :5, :6, :7)";


        OracleConnection con = new OracleConnection(oradb);
        con.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = con;
        cmd.CommandText = insertquery;
        try
        {

            cmd.Parameters.Add(new OracleParameter("1", OracleDbType.Decimal, ParameterDirection.ReturnValue));
            cmd.Parameters.Add(new OracleParameter("2", OracleDbType.Varchar2, txtBoxClientName.Text, ParameterDirection.Input));
            cmd.Parameters.Add(new OracleParameter("3", OracleDbType.Varchar2, txtBoxClientCity.Text, ParameterDirection.Input));
            cmd.Parameters.Add(new OracleParameter("4", OracleDbType.Varchar2, txtBoxClientCountry.Text, ParameterDirection.Input));
            cmd.Parameters.Add(new OracleParameter("5", OracleDbType.Varchar2, txtBoxClientNumber.Text, ParameterDirection.Input));
            cmd.Parameters.Add(new OracleParameter("6", OracleDbType.Varchar2, txtBoxClientURL.Text, ParameterDirection.Input));
            cmd.Parameters.Add(new OracleParameter("7", OracleDbType.Varchar2, comboClientStatus.Text, ParameterDirection.Input));

            cmd.ExecuteNonQuery();

            MessageBox.Show("Client has been added");
            con.Close();
            Close();
        }
        catch (OracleException e1)
        {
            MessageBox.Show("Error: " + e1.Message);
        }
        catch (ArgumentException e2)
        {
            MessageBox.Show("Error: " + e2.Message);
        }
        finally
        {                
            cmd.Dispose();
            con.Dispose();
        }

答案 1 :(得分:0)

像那样:

Storyboard