当前位置:数据库 > Oracle >>

C# 数据库的基本操作(oracle)

配置文件如下:
 

<appSettings> 
        <add key="ServiceName" value="lev2" /> 
        <add key="UserID" value="DBNAME" /> 
        <add key="UserPsw" value="DBPASSWORD" /> 
</appSettings> 
 
基本操作类如下:
[csharp]
using System; 
using System.Data; 
using System.Data.OracleClient; 
using System.Configuration; 
using System.Collections; 
using System.Text; 
namespace Common 

    /// <summary> 
    /// CommonClass 的摘要说明 
    /// </summary> 
    public class OracleServerDAL 
    { 
        OracleConnection connect = null; 
        OracleCommand command = null; 
        OracleDataReader reader = null; 
        OracleDataAdapter adapter = null; 
        DataSet ds = null; 
 
        public OracleServerDAL() 
        { 
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        } 
 
        public string GetConnStr() 
        { 
            string[] configStrings = ConfigHelper.ReadDBConfig(); 
 
            string serviceName = ""; 
 
            string userID = ""; 
 
            string userPSW = ""; 
 
            if (configStrings[0] == null || configStrings[1] == null || configStrings[2] == null) 
            { 
                return ""; 
            } 
 
            serviceName = ConfigHelper.GetAppConfig("ServiceName"); 
 
            userID = configStrings[1]; 
 
            userPSW = configStrings[2]; 
 
            string constr = string.Format("DATA SOURCE={0};user={1};password={2};", serviceName, userID, userPSW); 
 
            return constr; 
        } 
 
        /// <summary> 
        /// 建立与数据库的连接 
        /// </summary> 
        public void connectionData() 
        { 
            try 
            { 
                if (connect == null) 
                { 
                    string constr = GetConnStr(); 
                    if (string.IsNullOrEmpty(constr)) 
                    { 
                        connect = new OracleConnection(DefineConst.constr); 
                    } 
                    else 
                    { 
                        connect = new OracleConnection(constr); 
                    } 
                } 
 
                if (connect.State == ConnectionState.Closed) 
                { 
                    connect.Open(); 
                } 
            } 
            catch (Exception e) 
            { 
                Console.Write(e.Message); 
            } 
             
        } 
 
    /// <summary> 
    /// 获取数据库连接 
    /// </summary> 
    /// <returns></returns> 
    public static OracleConnection GetConnection() 
    { 
        return new OracleConnection(DefineConst.constr); 
    } 
 
        /// <summary> 
        /// 获取数据库连接字符串 
        /// </summary> 
        /// <returns></returns> 
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,