C#连接oracle实例

1.在C#中使用oracle需要 System.Data.OracleClient.dll

2.实例:

 

private string connStr = "data source=WFD;User Id=cas;Password=cas;";
private OracleConnection conn = null;

/// <summary>
        /// 创建连接
        /// </summary>
        /// <returns></returns>
        public OracleConnection getConnection()
        {
            try
            {
                if (conn == null) 
                {
                    conn = new OracleConnection(connStr);
                }
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                return conn;
            }
            catch (OracleException e)
            {
                throw e;
            }
        }

/// <summary>
        /// 关闭连接
        /// </summary>
        public void closeConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }

OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "select t.lockflag from cards t WHERE t.cardno = '" + cardno + "'";
                cmd.Connection = this.getConnection();
                OracleDataReader r = cmd.ExecuteReader();
                if (r.Read())
                {
                    string lockstate = r[0].ToString();
                    if ("".Equals(lockstate) || lockstate.Equals("0"))
                    {
                        lblStateResult.Text = "未绑定";
                    }
                    else if (lockstate.Equals("1"))
                    {
                        lblStateResult.Text = "已绑定";
                    }
                }

 

 

你可能感兴趣的:(oracle)