请问如何在C#中如何连接Oracle数据库?最好有现成的代码,谢谢。

Cell 2005-09-27 02:29:10
刚开始用C#,不好意思了。
...全文
1132 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jwd987654321 2005-11-26
OLE DB, OleDbConnection (.NET)


Standard security:

"Provider=msdaora;Data Source=MyOracleDB;User Id=UserName;Password=asdasd;"
This one's from Microsoft, the following are from Oracle
Standard Security:

"Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=Username;Password=asdasd;"



Trusted Connection:

"Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1;"
回复
jwd987654321 2005-11-26
OracleConnection (.NET)


Standard:

"Data Source=MyOracleDB;Integrated Security=yes;"
This one works only with Oracle 8i release 3 or later
Specifying username and password:

"Data Source=MyOracleDB;User Id=username;Password=passwd;Integrated Security=no;"
This one works only with Oracle 8i release 3 or later
Declare the OracleConnection:

C#:
using System.Data.OracleClient;
OracleConnection oOracleConn = new OracleConnection();
oOracleConn.ConnectionString = "my connectionstring";
oOracleConn.Open();

回复
smallMage 2005-11-26
添加NET引用System.Data.OracleClient.dll
回复
kuangluokuangluo 2005-11-26
从http://www.microsoft.com/downloads/details.aspx?FamilyID=4f55d429-17dc-45ea-bfb3-076d1c052524&DisplayLang=en
下载 oracle数据库提供程序
先引用 using System.Data.OracleClient;
string str ="Password=aptech;User ID=user01;Data Source=oemrep;Persist Security Info=True";
OracleConnection mycon=new OracleConnection(str);
mycon.Open();
回复
fenglik 2005-09-27
在解决方案资源管理器中引用添加NET引用System.Data.OracleClient.dll
回复
Cell 2005-09-27
我using System.Data.OracleClient;后怎么提示,OracleClient在System.Data中不存在啊?为什么?
回复
Cell 2005-09-27
我using System.Data.OracleClient;后怎么提示,OracleClient在System.Data中不存在啊?为什么?
回复
我不懂电脑 2005-09-27
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace 数字公路
{
/// <summary>
/// DragForm 的摘要说明。
/// </summary>
public class DragForm : System.Windows.Forms.Form
{
private System.Data.OracleClient.OracleDataAdapter oracleDataAdapter1;
private System.Data.OracleClient.OracleCommand oracleSelectCommand1;
private System.Data.OracleClient.OracleCommand oracleInsertCommand1;
private System.Data.OracleClient.OracleConnection oracleConnection1;
private 数字公路.DataSet1 dataSet11;
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public DragForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.oracleDataAdapter1 = new System.Data.OracleClient.OracleDataAdapter();
this.oracleSelectCommand1 = new System.Data.OracleClient.OracleCommand();
this.oracleInsertCommand1 = new System.Data.OracleClient.OracleCommand();
this.oracleConnection1 = new System.Data.OracleClient.OracleConnection();
this.dataSet11 = new 数字公路.DataSet1();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// oracleDataAdapter1
//
this.oracleDataAdapter1.InsertCommand = this.oracleInsertCommand1;
this.oracleDataAdapter1.SelectCommand = this.oracleSelectCommand1;
this.oracleDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "A0LJDATA71", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("路线代码", "路线代码"),
new System.Data.Common.DataColumnMapping("起点桩号", "起点桩号"),
new System.Data.Common.DataColumnMapping("止点桩号", "止点桩号"),
new System.Data.Common.DataColumnMapping("右边坡高度", "右边坡高度"),
new System.Data.Common.DataColumnMapping("右边坡坡度", "右边坡坡度")})});
//
// oracleSelectCommand1
//
this.oracleSelectCommand1.CommandText = "SELECT 路线代码, 起点桩号, 止点桩号, 右边坡高度, 右边坡坡度 FROM A0LJDATA71";
this.oracleSelectCommand1.Connection = this.oracleConnection1;
//
// oracleInsertCommand1
//
this.oracleInsertCommand1.CommandText = "INSERT INTO A0LJDATA71(路线代码, 起点桩号, 止点桩号, 右边坡高度, 右边坡坡度) VALUES (:路线代码, :起点桩号, :止点桩" +
"号, :右边坡高度, :右边坡坡度)";
this.oracleInsertCommand1.Connection = this.oracleConnection1;
this.oracleInsertCommand1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":路线代码", System.Data.OracleClient.OracleType.VarChar, 10, "路线代码"));
this.oracleInsertCommand1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":起点桩号", System.Data.OracleClient.OracleType.Double, 0, "起点桩号"));
this.oracleInsertCommand1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":止点桩号", System.Data.OracleClient.OracleType.Double, 0, "止点桩号"));
this.oracleInsertCommand1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":右边坡高度", System.Data.OracleClient.OracleType.Double, 0, "右边坡高度"));
this.oracleInsertCommand1.Parameters.Add(new System.Data.OracleClient.OracleParameter(":右边坡坡度", System.Data.OracleClient.OracleType.Double, 0, "右边坡坡度"));
//
// oracleConnection1
//
this.oracleConnection1.ConnectionString = "user id=sss;data source=szgl";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.A0LJDATA71;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(80, 96);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.TabIndex = 0;
//
// DragForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGrid1);
this.Name = "DragForm";
this.Text = "DragForm";
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion
}
}
回复
chaobeyond 2005-09-27
首先要引入using System.Data.OracleClient;命名空间......
其他的和SQL SERVER一样,只是换个名称,如SQL SERVER用SqlConnection,而oracle用OracleConnection 等.
回复
wangjingjing390 2005-09-27
[WebMethod(Description="登陆后用户获取可以执行的应用程序"), SoapHeader("CurrentUser")]
public DataSet GetApplications()
{
if(CurrentUser == null)
{
return null;
}
DataSet ds = new DataSet();
OracleConnection conn = this.GetConnection();
try
{
conn.Open();
OracleCommand cmd = this.GetCommand("UPGRADEPACK.GETAPPLICATIONS",conn);
cmd.Parameters["V_USER_ID"].Value = CurrentUser.UserID;
OracleDataAdapter ada = new OracleDataAdapter(cmd);
ada.Fill(ds);
}
finally
{
conn.Close();
}
return ds;
}
回复
wangjingjing390 2005-09-27
/// <summary>
/// 从配置文件获取Oracle连接
/// </summary>
/// <returns>Oracle连接</returns>
private OracleConnection GetConnection()
{
string connstring = System.Configuration.ConfigurationSettings.AppSettings["OracleConnection"];
OracleConnection conn = new OracleConnection(connstring);
return conn;
}
/// <summary>
/// 从CommandText和Connection生成OracleCommand
/// </summary>
/// <param name="CommandText">CommandText</param>
/// <param name="conn">已经打开的Oracle连接</param>
/// <returns>OracleCommand</returns>
private OracleCommand GetCommand(string CommandText, OracleConnection conn)
{
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = CommandText;
cmd.CommandType = CommandType.StoredProcedure;
OracleCommandBuilder.DeriveParameters(cmd);
return cmd;
}
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.