简体   繁体   English

谁能帮我如何在oracle中连接数据库和在c#中连接字符串

[英]can any one help me how to connect database in oracle and the connection string in c#

  protected void Page_Load(object sender, EventArgs e)
    {

        OracleConnection con = new OracleConnection();
        con.ConnectionString = "User id =test;password=test1;Datasource=oracle";
       myConnection.Open();

    }

Above is the code that I am using. 上面是我正在使用的代码。 It will be called on page_Load. 它将在page_Load上调用。

Follow below steps,below is my sample example: 请按照以下步骤操作,以下是我的示例示例:

1.In Web.config of your file add below string 1.在文件的Web.config中添加以下字符串

 <connectionStrings>
    <add name="CustomerDataConnectionString" connectionString="Data Source=.;User Id=*;Password=*;Integrated Security=SSPI;Initial Catalog=Northwind;OLEDB.NET=True" providerName="Oracle.DataAccess.Client"/>
  </connectionStrings>
//* must be filled with your credentials

2.Now in the code behind file,Import namespace for oracle client and Configuration manager for oracle client and below code 2.现在在文件后面的代码中,为Oracle客户端导入名称空间,为Oracle客户端导入配置管理器,以及以下代码

using System.Data.OracleClient;
using System.Data;
using System.Configuration;

3.Write below code in your Page_Load event:Cmd can be SQL command 3.在Page_Load事件中编写以下代码:Cmd可以是SQL命令

 static string strConnectionString = ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
 using (OracleConnection con = new OracleConnection(strConnectionString))
            {
try
                    {
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();

                        }

                        using (OracleDataAdapter da = new OracleDataAdapter(cmd))
                        {
                            table = new DataTable();
                            da.Fill(table);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
           }

Refer this link http://www.connectionstrings.com/ for more inforamtion 有关更多信息,请参考此链接http://www.connectionstrings.com/

Make sure you have included the required libraries, 确保已包含必需的库,

try using this code , 尝试使用此代码,

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

myConnectionString = "server=127.0.0.1;uid=root;" +
    "pwd=12345;database=test;";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

It's always better to have try-catch . try-catch总是更好。 that helps you track the exact error if you are stuck somewhere. 如果您被卡在某处,可以帮助您跟踪确切的错误。

        string oradb = "User id =test;password=test1;Datasource=oracle";
        OracleConnection conn = new OracleConnection(oradb);  
        conn.Open();
        using (OracleDataAdapter  a = new OracleDataAdapter(
                "SELECT id FROM emp1", conn))
            {
                DataTable t = new DataTable();
                a.Fill(t);
                // Render data onto the screen
                dataGridView1.DataSource = t;
            }

        conn.Dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM