如何在C#中创建一个oracleconnection方法,以便可以重用它来获取下一个数据库 [英] How to create a class of method for oracleconnection in a C# so that it can be reused for next database to be fetched
本文介绍了如何在C#中创建一个oracleconnection方法,以便可以重用它来获取下一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个非常新的c#编程初学者,因为我现在在c#中模糊不清,所以我会被要求在下面的代码中为OracleConnection数据库创建一个方法,这样它就可以了重复使用,没有再次打字。我附上了我的代码。感谢您的帮助。
Hi i am a pretty new beginner in c# programming i wi sh to seek help as i am blur in c# now... So i was asked to create a method for the OracleConnection database at the code below so that it could be reused without having ti type it once again. Hereby i attached my code. Thanks for the help.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess;
using Oracle.DataAccess.Client; //ODP.NET ORacle managed provider
namespace WindowsFormsApp7
{
public partial class MyApp : Form
{
public MyApp()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string OracleServer = "Data Source=mydb;"
+ "User Id=Config;Password=config123;"; // your username and password database server
OracleConnection conn = new OracleConnection(OracleServer);
conn.Open();
string orcl = @"Select * FROM mydatabase";
OracleCommand cmd = new OracleCommand(orcl);
cmd.Connection = conn;
OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
listBoxdB.DataSource = dt;
listBoxdB.ValueMember = "Database_id";
listBoxdB.DisplayMember = "Location";
this.dataGridView1.DataSource = dt;
this.dataGridView1.Refresh();
conn.Close();
conn.Dispose();
}
private void listBoxdB_SelectedIndexChanged(object sender, EventArgs e)
{
string strDbID = Convert.ToString(listBoxdB.SelectedValue);
string strDbLocation = Convert.ToString(listBoxdB.GetItemText(listBoxdB.SelectedItem));
string strSql = @"select Name, id from my_source
where id not in (select id
from new_database
where database_id = " + strDbID + ")";
string OracleServer = "Data Source=mydb;"
+ "User Id=config;Password=config123;";
OracleConnection conn = new OracleConnection(OracleServer);
conn.Open();
OracleCommand cmd = new OracleCommand(strSql);
cmd.Connection = conn;
OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
try
{
da.Fill(dt);
this.listBoxApp.DataSource = dt;
this.listBoxApp.ValueMember = "Application_id";
this.listBoxApp.DisplayMember = "Application_Name";
}
catch { }
}
private void listBoxApp_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
我有什么试过:
我一直在尝试创建一个类,但c#似乎不像我之前学过的其他编程,如c和c ++。 ..
What I have tried:
I've been tried creating a class but c# doesn't seems like the other programming i have learnt before such as c and c++...
推荐答案
如果我正确理解你的问题,我不会尝试存储连接以供以后重用。相反,我会让连接池来处理缓存连接对象。请查看 Oracle Data Provider for .NET的功能 [< a href =https://docs.oracle.com/cd/B19306_01/win.102/b14307/featConnecting.htm#i1006393\"target =_ blanktitle =New Window> ^ ]
连接字符串本身的内容,您可以存储它以便快速访问,例如在配置文件中,在单个静态字符串变量等中。
例如,看看在C#中读取配置设置的四种方法 [ ^ ]
If I understand your question correctly, I wouldn't try to store the connection for later reuse. Instead I would let connection pooling to take care of caching the connection objects. Have a look at Features of Oracle Data Provider for .NET[^]
What comes to the connection string itself, you can store it for quick access for example in a configuration file, in a single, static string variable etc.
For example, have a look at Four Ways to Read Configuration Setting in C#[^]
这篇关于如何在C#中创建一个oracleconnection方法,以便可以重用它来获取下一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文