闲齁齁
2022-01-05 16:25
采纳率: 50%
浏览 9

winform连接Oracle数据库

winform怎么连接Oracle数据库,

            //连接数据库
            string connString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.xxx.xxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ASRS)));Persist Security Info=True;User ID=asrs_bd_bf_1;Password=asrs;";

            OracleConnection conn = new OracleConnection(connString);
            conn.Open();

我这样写的连接数据库的方法,为啥到open的时候就显示应用程序处于中断模式

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • 神仙别闹 2022-01-05 16:32
    最佳回答
    using System;
    using System.Data;
    using Oracle.ManagedDataAccess.Client;
    
    namespace ODP.NET
    {
        class Program
        {
            static void Main(string[] args)
            {
                OracleConnection conn = null;
                try
                {
                    conn = OpenConn();
                    var cmd = conn.CreateCommand();
                    cmd.CommandText = "select * from s_awb_master where rownum=1";
                    cmd.CommandType = CommandType.Text;
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine(string.Format("AwbPre:{0},AwbNo:{1}", reader["AwbPre"], reader["AwbNo"]));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    CloseConn(conn);
                }
                Console.Read();
            }
    
    
            static OracleConnection OpenConn()
            {
                OracleConnection conn = new OracleConnection();
                conn.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=***.***.***.***)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=***)));Persist Security Info=True;User ID=***;Password=***;";
                conn.Open();
                return conn;
            }
    
            static void CloseConn(OracleConnection conn)
            {
                if (conn == null) { return; }
                try
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    conn.Dispose();
                }
            }
        }
    }
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题