从oracle数据库绑定组合框中的列

时间:2015-02-26 13:49:10

标签: c# oracle visual-studio-2012

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 Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;

namespace LibraryManagementSystem
{
    public partial class UpdateBook : Form
    {
        public UpdateBook()
        {
            InitializeComponent();
            FillCombo();
        }

        string ConString = "DATA SOURCE=XE;PERSIST SECURITY INFO=True;USER ID=ROOT;PASSWORD=ROOT;";

        void FillCombo()
        {
            string query = "SELECT * FROM Author ;";
            OracleConnection con = new OracleConnection(ConString);
            OracleCommand cmd = new OracleCommand(query, con);
            OracleDataReader myReader;

            try
            {
                con.Open();
                myReader = cmd.ExecuteReader();  //exception due to this line

                while (myReader.Read())
                {
                    comboBox1.Items.Add(myReader.GetString(myReader.GetOrdinal("AuthorName")));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OracleConnection con = new OracleConnection(ConString);
            OracleDataAdapter oda = new OracleDataAdapter("SELECT * FROM Book where Title='" + textBox11.Text + "'", con);
            DataTable dt = new DataTable();
            oda.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex>=0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                textBox1.Text = row.Cells[1].Value.ToString();
                textBox2.Text = row.Cells[2].Value.ToString();
                textBox3.Text = row.Cells[3].Value.ToString();
                textBox4.Text = row.Cells[4].Value.ToString();
                textBox5.Text = row.Cells[5].Value.ToString();
                textBox6.Text = row.Cells[6].Value.ToString();
                textBox7.Text = row.Cells[7].Value.ToString();
                textBox8.Text = row.Cells[8].Value.ToString();
                textBox9.Text = row.Cells[9].Value.ToString();
                textBox10.Text = row.Cells[10].Value.ToString();
            }
        }
    }
}

在上面的代码中,我想通过Author表的AuthorName列填充comboBox1。 但我得到以下例外:

  

Oracle.ManagedDataAccess.Client.OracleException(0x0000038F):   ORA-00911:无效字符。

它表明代码

中标记的行中出现异常

请帮助我。

1 个答案:

答案 0 :(得分:0)

在select语句中取出分号 这样做:string query = "SELECT * FROM Author ;";
进入:string query = "SELECT * FROM Author";