使用Oledb将数据Oracle Table加载到C#组合框 [英] Load data Oracle Table to C# combobox using Oledb

查看:103
本文介绍了使用Oledb将数据Oracle Table加载到C#组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Oracle数据库中创建了一个表 buy_unit

hello i have create a table buy_unit in oracle database

创建表buy_unit( unit_id NUMBER(10)不为null PRIMARY KEY, unit_name VARCHAR2(10)不为null );

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

并插入值

插入BUY_UNIT values(001,'Liter'); desc SELL_BUY_UNIT;

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

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

then create a combo box combobox1 in C# now can load buy_unit table data in combobox ? i have use the connection:

OleDbConnection con =新的OleDbConnection("Provider = MSDAORA;数据源= XE; User ID = user1; password = pssword");

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

推荐答案

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

Well, you can see below code which is refering SQLConnection. You can change as per your DB may be OracleConnection or so. While using SQL, we have pass connection string with UID and PWD.

对于Oracle 连接字符串为

For Oracle have connection string as

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块

and try to use using block when you open DB connection as below

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

Oracle的代码.请原谅语法.

Code for Oracle. Excuse for syntax.

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);
            }
        }

这篇关于使用Oledb将数据Oracle Table加载到C#组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆