【.NET】C#連線oracle實現登入及查詢
阿新 • • 發佈:2019-01-08
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
新建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 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(); } } }
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(); } } }