使用 Oledb 将数据 Oracle Table 加载到 C# 组合框

Load data Oracle Table to C# combobox using Oledb

你好,我在 Oracle 数据库

中创建了一个 table buy_unit

CREATE TABLE buy_unit( unit_id NUMBER(10) not null PRIMARY KEY, unit_name VARCHAR2(10) not null );

并插入值

INSERT INTO BUY_UNIT values(001,'Liter'); desc SELL_BUY_UNIT;

然后在 C# 中创建一个组合框 combobox1 现在可以在组合框中加载 buy_unit table 数据了吗?我已经使用连接:

OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=XE;User ID=user1;password=pssword");

好吧,您可以看到下面的代码引用了 SQLConnection。您可以根据您的数据库进行更改,可能是 OracleConnection 左右。在使用 SQL 时,我们传递了带有 UID 和 PWD 的连接字符串。

对于甲骨文 连接字符串为

string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";

public void BindComboBox()
{
    SqlConnection con = new SqlConnection(@"server=ServerName; database = DBName ;  User Id=sa; Password=PeaTeaCee5#");
    con.Open();
    string strCmd = "select desire column from table";
    SqlCommand cmd = new SqlCommand(strCmd, con);
    SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    cmd.ExecuteNonQuery();
    con.Close();

    cbSupportID.DisplayMember = "name to display";
    cbSupportID.ValueMember = "id";       
    cbSupportID.DataSource = ds;

    cbSupportID.Enabled = true;

}

并尝试在打开数据库连接时使用 using 块,如下所示

using (OracleConnection objConn = new OracleConnection(con))
 {
   \ you code
   \ do your stuff
 }

Oracle 代码。请原谅语法。

public  void  Fillcombo()
        {
            string oradb = " Data Source=xe;User Id=dbname;Password=pws; ";
            string query = "select id , name from table";
            OracleConnection condatabase = new OracleConnection(oradb);
            OracleCommand cmddatabase = new OracleCommand(query, condatabase);

            try
            {
                condatabase.Open();
                OracleDataReader myReader = cmddatabase.ExecuteReader(); ;
                myReader = cmddatabase.ExecuteReader();
                while (myReader.Read())
                {
                    string sname = myReader.GetInt32(0).ToString();
                    comboBox1.Items.Add(sname.ToString());
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }