【.NET】C#连接oracle实现登录及查询

C#中连接Oracle数据库的方法有: 微软的System.Data.OracleClient (可直接引用,已过时),Oracle的Oracle.DataAccess.Client (ODP.net;区分X86与X64) 和Oracle的Oracle.ManagedDataAccess.dll(最优)  【需要下载dll,在项目中引用】附下载地址:https://download.csdn.net/download/bianhua_c/10413236  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wdemo
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }
            String name, pwd;
        private void button1_Click_1(object sender, EventArgs e)
        {
            //数据源
            string connString = " Provider = OraOLEDB.Oracle.1; Data Source = (DESCRIPTION = (CID = GTU_APP)(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SID = ORCL)(SERVER = ORCL))); User Id = system;Password = sys";
            OleDbConnection conn = new OleDbConnection(connString);//实例化连接对象
            conn.Open();//打开连接
           
            OleDbCommand cmdd = conn.CreateCommand();
            cmdd.CommandText = "select * from userinfo where name = '" + this.TextName.Text + "' and pwd = '" + this.textPwd.Text + "'";
            OleDbDataReader oleRead = cmdd.ExecuteReader();//注意表在哪个用户下

            //测试连接oracle数据库
            //try{
            //    conn.Open();
            //    MessageBox.Show("连接成功");
            //}
            //catch (Exception ex){
            //    MessageBox.Show("失败了哦");
            //}
            //finally{
            //    conn.Close();
            //}

            try
            {
                while (oleRead.Read())
                {
                     name = oleRead["name"].ToString();
                     pwd = oleRead["pwd"].ToString();
                }
                if (this.TextName.Text.Trim() == name.Trim() && this.textPwd.Text.Trim() == pwd.Trim())
                {
                    MessageBox.Show("欢迎登录HIS医院信息系统! ", "SUCCESS");
                    Show sw = new Show();//跳转窗体
                    sw.Show();
                }
                else
                {
                    MessageBox.Show("您的用户名或密码不正确! ", "ERROR");
                }
            }
            catch{
                MessageBox.Show("数据库无法连接!");
            }finally{
                conn.Close();//关闭连接
            }
        }

        private void reset_Click(object sender, EventArgs e)
        {
            TextName.Text = "";
            textPwd.Text = "";
            TextName.Focus();
        }

    }   
}

新建Show窗体添加DataGridView控件,查询显示表中信息!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wdemo
{
    public partial class Show : Form
    {
        public Show()
        {
            InitializeComponent();

            string connection = " Provider = OraOLEDB.Oracle.1; Data Source = (DESCRIPTION = (CID = GTU_APP)(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SID = ORCL)(SERVER = ORCL))); User Id = system;Password = sys";//数据库的连接 数据源 用户 密码
            OleDbConnection coon = new OleDbConnection(connection);//建立数据库连接

            OleDbCommand cmd = new OleDbCommand("select * from userinfo",coon);//执行数据连接    如果想选某两个列可将*改为name,pwd 即 “select name,pwd from userinfo”

            DataSet ds1;
            ds1 = new DataSet();//定义数据集

            OleDbDataAdapter da1 = new OleDbDataAdapter(cmd);//取出数据表
            da1.Fill(ds1);//将数据加载到数据集中

            DataTable dt = ds1.Tables[0];//将数据放入表中

            coon.Close();//关闭数据库连接

            //遍历
            // //表为空则返回
            //下面的代码可对表进行操作 如果想直接显示可直接加上数据显示代码  下面的if  else代码就不用了
            if (dt == null)
            {
                return;
            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                }
            }
            //数据显示 在dataGridView中显示
            this.dataGridView1.DataSource = dt.DefaultView;
            this.dataGridView1.Refresh();
        }
   
     }  
}

你可能感兴趣的:(.NET)