記錄一下最近使用C#寫應(yīng)用程序遇到的一些知識(shí)
1.連接Oracle數(shù)據(jù)庫
首先:
using System.Data.OracleClient;
這個(gè)VS2010可能已經(jīng)不支持了,也就是說,引入這個(gè)命名空間以后會(huì)報(bào)錯(cuò)
Solution(解決方案):右擊項(xiàng)目->屬性,找到目標(biāo)框架,默認(rèn)是:.NET Framework 4 Client Profile,將它改為.NET Framework 4,然后,右擊項(xiàng)目->添加引用,選擇.NET選項(xiàng)頁,找到該命名空間,選擇,確定就行了!
然后:
connection = "data source=CCC;user=AAA;password=BBB;";//初始化連接字符串
conn = new OracleConnection(connection); //連接數(shù)據(jù)庫
使用conn.Open()即可連接上數(shù)據(jù)庫,conn.Close()即可斷開連接
datasource是一個(gè)代號,指向你創(chuàng)建的Oracle數(shù)據(jù)庫,可以在啟動(dòng)欄里找到,user是你在該數(shù)據(jù)庫里面創(chuàng)建的用戶名,默認(rèn)的有sys,system等,如果你在安裝的時(shí)候創(chuàng)建了數(shù)據(jù)庫,密碼就在你安裝的時(shí)候確定了!
2.Oracle數(shù)據(jù)庫讀取數(shù)據(jù)的一般方式
第一種:使用OracleDataReader
String sql = "select name from course where id not in (select courseId from coursechoose where studentId = '" + id + "')";
OracleCommand command = conn.CreateCommand();
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
String courseName = "";
while (reader.Read())
{
//獲取課程Id
String course_choosed = reader.GetString(0);
courseName = courseName + course_choosed + "|";
}
第二種:使用OracleDataAdapter
String command = "select studentId from coursechoose where courseId = '" + courseId + "'";
OracleCommand cmd = new OracleCommand(command, conn); //寫入sql語句
OracleDataAdapter da = new OracleDataAdapter(cmd); //建立適配器
DataSet ds = new DataSet(); //建立Dataset
da.Fill(ds);
3.MessageBox的使用(顯示確定取消按鈕)
MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
DialogResult dr = MessageBox.Show("你確定刪除該用戶?", "刪除提醒", messButton);
if(dr == DialogResult.OK){}
4.DataGridView的使用
a.顯示數(shù)據(jù)庫返回?cái)?shù)據(jù)
DataSet allStudent = db.studentSearch(courseId);
dataGridView1.DataSource = allStudent.Tables[0]; //將數(shù)據(jù)放入到DataGridView1中
b.獲取單元格中的數(shù)據(jù)
第一步:獲取選中行
int indexId = dataGridView1.CurrentCell.RowIndex;//如果沒有選中會(huì)返回負(fù)值?
第二步:獲取主鍵(根據(jù)顯示內(nèi)容而定)
String studentId = dataGridView1.Rows[indexId].Cells[0].Value.ToString();
5.ListView添加刪除子項(xiàng)
ListViewItem lvi = new ListViewItem();
lvi.Text = courses[i];
listView.Items.Add(lvi);
ListViewItem add = listView2.SelectedItems[0];
listView.Items.Remove(add);
就寫這么多吧,有時(shí)間再補(bǔ)充!