使用c#将数据插入Oracle数据库

时间:2016-09-05 20:38:13

标签: c# oracle

我收到此错误" ora-00928缺少select keyword"使用按钮提交查询时。我对其他按钮有其他查询,并且select语句有效,但由于某种原因,insert语句不起作用。 我已经看到有关此错误的其他帖子,但似乎没有任何帮助我的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Oracle
{
    public partial class Register : Form
    {
        string name;
        int pass;
        int repass;
        string email;
        public Register()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=DESKTOP-HQCK6F1:1521/CTECH;Persist Security Info=True;User ID=system;Password=G4ming404;Unicode=True");
        OleDbCommand cmd = new OleDbCommand();

        private void button1_Click(object sender, EventArgs e)
        {
            name = txtname.Text;
            pass = Convert.ToInt32(txtpass.Text);
            repass = Convert.ToInt32(txtrepass.Text);
            email = txtemail.Text;
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO SYSTEM.CUSTOMER('CUSTOMER_ID', 'CUSTOMER_NAME', 'CUSTOMER_EMAIL', 'CUSTOMER_PASSWORD')" + "VALUES('%"+ null + "%','%'" + txtname.Text + "%','%'" + txtemail.Text + "%','%'" + txtpass.Text + "%')";
            con.Open();

            if (pass == repass)
            {
                int rowsUpdated = cmd.ExecuteNonQuery();
                if (rowsUpdated == 0)
                {
                    MessageBox.Show("Record not inserted");
                }
                else {
                    MessageBox.Show("Success!");
                   }

                MessageBox.Show("User has been created");
                this.Close();
                Form1 login = new Form1();
                login.Show();

            }
            else {
                MessageBox.Show("Password mismatch");
            }
            con.Dispose();
        }

2 个答案:

答案 0 :(得分:1)

您的查询中存在一些问题 首先,您不需要围绕列名称使用单引号。仅当您的任何列与保留关键字具有相同名称时,才需要双引号。

第二个问题是输入框文本与查询命令的字符串连接。应该不惜一切代价避免这种情况,因为它是解析问题和sql注入黑客的根源。请改用参数。

最后,您的OleDbConnection应该是您的方法的本地,并且在using语句中,以确保在异常的情况下正确处理非托管资源

private void button1_Click(object sender, EventArgs e)
{
    name = txtname.Text;
    pass = Convert.ToInt32(txtpass.Text);
    repass = Convert.ToInt32(txtrepass.Text);
    email = txtemail.Text;

    if (pass != repass)
    {
        MessageBox.Show("Password mismatch");
        return;
    }
    string cmdText = @"INSERT INTO SYSTEM.CUSTOMER 
                       (CUSTOMER_NAME, CUSTOMER_EMAIL, CUSTOMER_PASSWORD) 
                       VALUES(?,?,?)";
    using(OleDbConnection con = new OleDbConnection(.......))
    using(OleDbCommand cmd = new OleDbCommand(cmdText, con))
    {
         con.Open();
         cmd.Parameters.Add("p1", OleDbType.VarChar).Value = txtname.Text;
         cmd.Parameters.Add("p2", OleDbType.VarChar).Value = txtemail.Text;
         cmd.Parameters.Add("p3", OleDbType.VarChar).Value = txtpass.Text ;
         int rowsUpdated = cmd.ExecuteNonQuery();
         if (rowsUpdated == 0)
         {
             MessageBox.Show("Record not inserted");
         }
         else 
         {
            MessageBox.Show("Success!");
            MessageBox.Show("User has been created");
         }
    }
    Form1 login = new Form1();
    login.Show();
}

我还删除了CUSTOMER_ID字段的参数传递。这似乎是由Oracle自动计算的字段(序列?),因此您不需要为它提供值。

最后一个建议。不要在数据库中以纯文本格式存储密码。这是一个非常严重的安全风险。您应该阅读Best way to store passwords in a database

答案 1 :(得分:0)

您的CommandText似乎有误。为什么用'%'包装所有值?你应该将null作为字符串传递给你。与null的联系不会更改任何字符串值。

我认为应该是:

cmd.CommandText = $@"INSERT INTO SYSTEM.CUSTOMER
('CUSTOMER_ID', 'CUSTOMER_NAME', 'CUSTOMER_EMAIL', 'CUSTOMER_PASSWORD')
VALUES(NULL, {txtname.Text}, {txtemail.Text}, {txtpass.Text})";