.net連接oracle數據庫---Shinepans
常見問題:
1.缺少引用 解決辦法 ,添加引用:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Oracle.DataAccess;
2.連接字符串:
globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
globals.username = this.username.Text.Trim();
globals.databaseName = this.databaseName.Text.Trim();
這是我的一貫風格,喜歡用globals類管理通用的字符串.方便管理,通過上面的代碼就可以知道連接字符串了.
3.查詢:
查詢格式如上
運行結果:
1.登錄:
2.登錄結果:
3.顯示主界面 (測試)
4.查詢數據庫:
至此,查詢功能已完成
關鍵點:
1.查詢語句中,不要帶分號,會提示關鍵字錯誤
2.引用要添加好:
using System.Data.Common;
using Oracle.DataAccess;
3.連接字符串要寫好:
"Data Source=shinepans;uid=system;pwd=123;unicode=True"
上面的改寫成你的
代碼片:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Net3._5_oracleTest
{
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginForm());
}
}
}
LoginForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Net3._5_oracleTest
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
globals.username = this.username.Text.Trim();
globals.databaseName = this.databaseName.Text.Trim();
OracleConnection conn = new OracleConnection(globals.connectionString);
try
{
conn.Open();
MessageBox.Show("已連接到Oracle數據庫!");
MainForm mf = new MainForm();
this.Hide();
mf.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}finally
{
conn.Close();
}
}
}
}
MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Net3._5_oracleTest
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
globals.connectionString = "Data Source="+this.databaseName.Text.Trim()+";uid="+this.username.Text.Trim()+";pwd="+this.password.Text.Trim()+";";
globals.username = this.username.Text.Trim();
globals.databaseName = this.databaseName.Text.Trim();
OracleConnection conn = new OracleConnection(globals.connectionString);
try
{
conn.Open();
MessageBox.Show("已連接到Oracle數據庫!");
MainForm mf = new MainForm();
this.Hide();
mf.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}finally
{
conn.Close();
}
}
}
}
GridView_UserInfo.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Oracle.DataAccess;
namespace Net3._5_oracleTest
{
public partial class GrideViews_UserInfo : Form
{
public GrideViews_UserInfo()
{
InitializeComponent();
}
private void GrideViews_UserInfo_Load(object sender, EventArgs e)
{
string sql1 = "SELECT * FROM infoofuser";
OracleConnection myconn = new OracleConnection(globals.connectionString);
OracleCommand mycmd = myconn.CreateCommand();
mycmd.CommandText = sql1;
myconn.Open();
OracleDataAdapter oraDA = new OracleDataAdapter(mycmd);
DataSet ds = new DataSet();
oraDA.Fill(ds);
myconn.Close();
DataTable dtbl = ds.Tables[0];
this.dataGridView1.DataSource = dtbl;
}
}
}
總結:
如果你的引用沒有我說的兩個,需要在項目裡找引用,然後右鍵選擇添加
這個是大家都知道的,oracle的各種錯誤讓人煩惱不已,各種錯誤,幸好有百度,可以查詢錯在哪裡,所以不要害怕困難,多總結.