asp 连接 oracle
ASP.Net to Oracle Connectivity
ASP.Net到Oracle的连接
Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers to develop an ASP.NET client connecting to an Oracle database server.This article contain very basic and simple steps for the connectivity.
最近,我不得不开发一个连接到Oracle数据库的ASP.NET应用程序。当我第一次这样做时,我不得不解决一些问题。 本文将帮助此类开发人员开发连接到Oracle数据库服务器的ASP.NET客户端。本文包含非常基本且简单的连接步骤。
I came across this article : http://msdn.microsoft.com/en-us/library/ms971506.aspx which helped enourmously, and used quite a bit of it in achieving my own solution. The code and conetent below is a combination from the MS article and my own work in making it work.
我碰到了这篇文章: http : //msdn.microsoft.com/en-us/library/ms971506.aspx ,它提供了极大的帮助,并在实现我自己的解决方案中使用了大量。 下面的代码和相关内容是MS文章和我自己的工作的结合体。
It is assumed that developers know the concept of TNSNAMe.ora file on Oracle /admin/Netwrok folder where connetions for the database with IP and Port are declared. the same database connection need to use here.
假定开发人员知道Oracle / admin / Netwrok文件夹中TNSNAMe.ora文件的概念,在该文件中声明了具有IP和端口的数据库连接。 在这里需要使用相同的数据库连接。
Objective :
目的:
1. Connect to Oracle Database
1.连接到Oracle数据库
2. Retrieve the data from database
2.从数据库中检索数据
1. Define connectionstring to Web.Config
1.定义到Web.Config的连接字符串
<appSettings>
<add key="conStr" value="Data Source=Name;User Id=UID; Password=Password"></add>
</appSettings>
2. Import namespace for oracleconnections
2.为oracleconnections导入名称空间
Add a reference to System.Data.OracleClient.d
添加对System.Data.OracleClient.d的引用 ll(用于Oracle的Microsoft .NET Framework数据提供程序)到您的项目
using System.Data.OracleClient;
3 .Define Oracle connection object
3,定义Oracle连接对象
OracleConnection oraconn = null;
4.Code Behind Add function for connect to database
4,添加功能背后的代码用于连接数据库
private bool ConnectDatabase()
{
oraconn = new OracleConnection(ConfigurationSettings.AppSettings["conStr"]);
if (oraconn.State == System.Data.ConnectionState.Closed)
{
oraconn.Open();
}
return true;
}
5. Function to call command from database
5.从数据库调用命令的功能
Create an OracleCommand object. Set its Connection property to the connection created . Set its CommandType property to CommandType.Text
创建一个OracleCommand对象。 将其连接属性设置为创建的连接。 将其CommandType属性设置为CommandType.Text
private string GetData(string ID)
{
try
{
OracleDataReader drec;
OracleCommand cmd = new OracleCommand("Select colname from tablename where ID = '" + ID + "'", oraconn);
drec = cmd.ExecuteReader();
drec.Read();
if ( drec.HasRows )
{
return drec.GetValue(0).ToString();
}
else
return "E:No Data Present";
}
catch (Exception ex)
{
return ex.Message;
}
}
6. Function to call Procedure from database
6.从数据库调用过程的功能
Create an OracleCommand object. Set its Connection property to the connection created . Set its CommandType property to CommandType.StoredProcedur
创建一个OracleCommand对象。 将其连接属性设置为创建的连接。 将其CommandType属性设置为CommandType.StoredProcedur Ë
private string GetDataBySP(string ID)
{
try
{
OracleDataReader drec;
OracleCommand cmd = new OracleCommand("SP_NAME", oraconn);
cmd.CommandType = CommandType.StoredProcedure ;
drec = cmd.ExecuteReader();
drec.Read();
if ( drec.HasRows )
{
return drec.GetValue(0).ToString();
}
else
return "E:No Data Present";
}
catch (Exception ex)
{
return ex.Message;
}
}
7. Call above functions from Code
7.从代码中调用上述函数
ConnectDatabase();
GetData("1");
8. Remember to close the connection when you are done
8.完成后,请记住关闭连接
oraconn.Close();
oraconn.Close();
9. If you are retrieving a result set, create a DataSet, DataTable, or DataReader
9.如果要检索结果集,请创建一个DataSet,DataTable或DataReader
Open the connection and execute the stored procedure using one of the Execute methods of the OracleCommand object shown below:
使用以下所示的OracleCommand对象的Execute方法之一打开连接并执行存储过程:
2.ExecuteNonQuery : Executes a query or procedure that does not return a result set returning the number of rows affected.
2. ExecuteNonQuery :执行不返回结果集的查询或过程,该结果集不返回受影响的行数。
3.ExecuteOracleNonQuery : Executes a query, returning the number of rows affected.
3. ExecuteOracleNonQuery :执行查询,返回受影响的行数。
This method also uses an OracleString parameter to return the row id for the last row modified by an UPDATE, INSERT, or DELETE query.
此方法还使用OracleString参数返回由UPDATE,INSERT或DELETE查询修改的最后一行的行ID。
4.ExecuteScalar: Executes a query or procedure and returns either the return value or the value of the first column of the first row of the result set as a .NET Framework data type.
4. ExecuteScalar :执行查询或过程,并以.NET Framework数据类型返回返回值或结果集第一行的第一列的值。
5.ExecuteOracleScalar : Executes a query or procedure and returns either the return value or the value of the first column of the first row of the result set as an OracleType data type
5. ExecuteOracleScalar :执行查询或过程,并以OracleType数据类型返回返回值或结果集第一行的第一列的值
You can also go thorugh the Article for Asp.net to SQl connectivity, which is helpul for new deveopers.
您也可以阅读Asp.net到SQl连接的文章,这对新开发人员是有帮助的。
https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_218-Tutorial-for-new-developers-in-Asp-net-connectivity-with-Sql-server-for-vb-C-and-fill-the-Datagrid-or-Gridview.html https://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_218-Tutorial-for-new-developers-in-Asp-net-connectivity-with-Sql-server-for-vb -C-and-fill-the-Datagrid-或-Gridview.html翻译自: https://www.experts-exchange.com/articles/5165/ASP-Net-to-Oracle-Connectivity.html
asp 连接 oracle