- 論壇徽章:
- 0
|
最近因工作需要,用C#做了些應用,現(xiàn)對ADO.NET的數(shù)據(jù)庫訪問作一個小結,以供大家交流學習,遲些時候貼上自己的一些代碼。
ADO.NET的數(shù)據(jù)訪問對象是以下幾個:
1、Connection對象:與數(shù)據(jù)源建立連接,連接sql server7.0 或更新版本數(shù)據(jù)庫用SqlConnection,連接OLEDB數(shù)據(jù)源使用OledbConnection.
2、Command 對象:對數(shù)據(jù)源執(zhí)行SQL命令并返回結果,SQL Server7.0或更新版本用SqlCommand,OLE DB數(shù)據(jù)源使用OledbCommand.
3、DataReader對象: 讀取數(shù)據(jù)源的數(shù)據(jù),只能將數(shù)據(jù)源的數(shù)據(jù)從頭到尾依次讀出,Sql server7.0或以上版本使用SqlDataReader,Oledb數(shù)據(jù)源使用OledbReader
4、DataAdapter對象:對數(shù)據(jù)源執(zhí)行操作并返回結果,在DataSet與數(shù)據(jù)源之間建立通信,將數(shù)據(jù)源中的數(shù)據(jù)寫入DataSet ,或根據(jù)DataSet中的數(shù)據(jù)必定數(shù)據(jù)源。Sql server7.0或以上版本使用SqlDataAdapter,Oledb 數(shù)據(jù)源使用OledbAdpater.
5、DataSet對象:服務器內存中的數(shù)據(jù)庫
6、DataView對象:用于顯示DataSet中的數(shù)據(jù)
從數(shù)據(jù)庫中讀取紀錄的另一種方法是使用Dataset對象和Dataadapter對象.Dataset是ADO.NET的主要組件之一,它用于緩存從數(shù)據(jù)源檢索到的數(shù)據(jù)信息。Dataadapter作為Dataset和數(shù)據(jù)源之間的橋接器,用于檢索和保存數(shù)據(jù)。 Dataadapter從數(shù)據(jù)庫中獲取數(shù)據(jù)后使用Fill方法把數(shù)據(jù)填充到Dataset中。下面以Sqldataadapter為例說明如何使用 Dataset對象和Dataadapter對象從數(shù)據(jù)庫中讀取記錄。執(zhí)行查詢的關鍵步驟如下:
1、創(chuàng)建與數(shù)據(jù)庫建立連接的Sqlconnection,傳遞連接字符串。
2、構造包含查詢語句的Sqldataadapter對象;
3、若要使用查詢結果填充Dataset對象,則調用命令Fill方法。
c#利用ado.net進行數(shù)據(jù)庫開發(fā)的基本步驟:
1、創(chuàng)建和數(shù)據(jù)庫連接的connection 對象。
2、配置DataAdapter對象并創(chuàng)建和操作數(shù)據(jù)集DataSet。
3、將數(shù)據(jù)庫中的表添加到DataSet中。
4、把數(shù)據(jù)集DataSet綁定到DataGrid上。利用DataAdapter 的Fill方法把數(shù)據(jù)填充到DataSet,最終的數(shù)據(jù)庫中的數(shù)據(jù)顯示在用戶界面的DataGrid中。
c#中從數(shù)據(jù)庫查詢記錄的方法分類:
一般使用兩種方法:
一種是通過DataReader對象直接訪問;另一種則是通過數(shù)據(jù)集Dataset和Dataadapter對象訪問.
使用ADO.NET的Datareader對象能從數(shù)據(jù)庫中檢索數(shù)據(jù)。檢索出來的數(shù)據(jù)形成一個只讀只進的數(shù)據(jù)流,存儲在客戶端的網絡緩沖區(qū)內。 Datareader對象的read方法可以前進到一下條記錄。在默認情況下,每執(zhí)行一次read方法只會在內存中存儲一條記錄系統(tǒng)的開銷非常少。
創(chuàng)建datareader之前必須先創(chuàng)建sqlcommand對象,然后調用該對象的executereader方法來構造sqldatareader對象,而不是直接使用構造函數(shù)。
下面的示例程序完成的功能是訪問sqlserver數(shù)據(jù)庫,并使用datareader從northwind數(shù)據(jù)中讀取記錄,并將查詢結果通過控制臺輸出。
using System;
using System.Data;
using System.Data.SqlClient;
namespace ReadDataFromDB{
class Class1{
static void Main(string[] args){
string myconn="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind";
//需要執(zhí)行的SQL語句
string mysql="select OrderID,CustomerID from Orders where CustomerID='CHOPS'";
//打開數(shù)據(jù)庫連接。
SqlConnection myconnection=new SqlConnection(myconn);
myconnection.Open();
//創(chuàng)建SqlCommand 對象
SqlCommand mycommand=new(mysql,myconnection);
//通過SqlCommand的ExecuteReader()方法構造DataReader 對象。
SqlDataReader myreader=mycommand.ExecuteReader();
while(myreader.read()){
Console.WriteLine(myreader.GetInt32(0)+","+myreader.GetString(1));
}
myreader.Close();
myconnection.Close();
}
}
}
VS2008下C#存儲圖片到sql2005數(shù)據(jù)庫中 收藏
表名:table_3
表結構
字段名 字段類型 其它
id int 自增長
i_image image
寫數(shù)據(jù)庫
view plaincopy to clipboardprint?
1. //寫入數(shù)據(jù)庫
2.
3. //創(chuàng)建數(shù)據(jù)庫連接字符串
4. string strConn = @"Data Source=.;Initial Catalog=test;User ID=sa assword=sa";
5.
6. //創(chuàng)建SQL語句
7. string sql = "insert into table_3 (i_image) values (@i_image)";
8. try
9. {
10. //創(chuàng)建SqlConnection對象
11. SqlConnection connection = new SqlConnection(strConn);
12.
13. //創(chuàng)建SqlCommand對象
14. SqlCommand command = new SqlCommand(sql, connection);
15.
16.
17. Byte[] bytBLOBData = getphoto(textEdit1.Text);
18.
19. ////創(chuàng)建參數(shù)對象
20. SqlParameter prm = new SqlParameter("@i_image", SqlDbType.Image, bytBLOBData.Length,
21. ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData);
22.
23. command.Parameters.Add(prm);
24.
25. //打開數(shù)據(jù)庫連接
26. connection.Open();
27.
28. command.ExecuteNonQuery();
29.
30. connection.Close();
31. }
32.
33. catch (Exception ex)
34. {
35. MessageBox.Show(ex.Message);
36. }
37. finally
38. {
39. MessageBox.Show("上傳完成!" ;
40. }
//寫入數(shù)據(jù)庫 //創(chuàng)建數(shù)據(jù)庫連接字符串 string strConn = @"Data Source=.;Initial Catalog=test;User ID=sa assword=sa"; //創(chuàng)建SQL語句 string sql = "insert into table_3 (i_image) values (@i_image)"; try { //創(chuàng)建SqlConnection對象 SqlConnection connection = new SqlConnection(strConn); //創(chuàng)建SqlCommand對象 SqlCommand command = new SqlCommand(sql, connection); Byte[] bytBLOBData = getphoto(textEdit1.Text); ////創(chuàng)建參數(shù)對象 SqlParameter prm = new SqlParameter("@i_image", SqlDbType.Image, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData); command.Parameters.Add(prm); //打開數(shù)據(jù)庫連接 connection.Open(); command.ExecuteNonQuery(); connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { MessageBox.Show("上傳完成!" ; }
view plaincopy to clipboardprint?
1. /// <summary>
2. /// 定義將圖片轉化為二進制代碼的函數(shù)getphoto()
3. /// </summary>
4. /// <param name="photopath">圖片路徑</param>
5. /// <returns>二進制代碼</returns>
6. public Byte[] getphoto(string photopath)
7. {
8. string str = photopath;
9. FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
10. Byte[] bytBLOBData = new Byte[file.Length];
11. file.Read(bytBLOBData, 0, bytBLOBData.Length);
12. file.Close();
13. return bytBLOBData;
14. }
/// <summary> /// 定義將圖片轉化為二進制代碼的函數(shù)getphoto() /// </summary> /// <param name="photopath">圖片路徑</param> /// <returns>二進制代碼</returns> public Byte[] getphoto(string photopath) { string str = photopath; FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read); Byte[] bytBLOBData = new Byte[file.Length]; file.Read(bytBLOBData, 0, bytBLOBData.Length); file.Close(); return bytBLOBData; }
讀數(shù)據(jù)庫
view plaincopy to clipboardprint?
1. //讀數(shù)據(jù)庫
2.
3. //創(chuàng)建數(shù)據(jù)庫連接字符串
4. string strConn = @"Data Source=.;Initial Catalog=test;User ID=sa assword=sa";
5.
6. //創(chuàng)建SQL語句
7. string sql = "select id,i_image from table_3";
8. try
9. {
10. //創(chuàng)建SqlConnection對象
11. SqlConnection connection = new SqlConnection(strConn);
12.
13. //打開數(shù)據(jù)庫連接
14. connection.Open();
15.
16. //創(chuàng)建SqlCommand對象
17. SqlCommand command = new SqlCommand(sql, connection);
18.
19. //創(chuàng)建DataAdapter對象
20. SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
21.
22. //創(chuàng)建DataSet對象
23. DataSet dataSet = new DataSet();
24.
25. //填充dataset
26. dataAdapter.Fill(dataSet, "table_3" ;
27.
28. int c = dataSet.Tables["table_3"].Rows.Count;
29.
30.
31. connection.Close();
32.
33. gridControl1.DataSource = dataSet.Tables["table_3"];
34.
35. }
36.
37. catch (Exception ex)
38. {
39.
40. MessageBox.Show(ex.Message);
41.
42. } .最簡單的方法:打印輸出
C#代碼 收藏代碼
1. Console.WriteLine("hello,come on baby");
Console.WriteLine("hello,come on baby");
這句話等同于java中的
Java代碼 收藏代碼
1. Syste.out.println("hello" ;
Syste.out.println("hello" ;
2.error list view
點擊視圖-錯誤列表(view-error list),打開錯誤視圖,這個沒啥好所的,自己實踐
3.控制臺輸出
C#代碼 收藏代碼
1. Debug.WriteLine("debug:hello" ;//僅在調試模式下運行
2. Trace.WriteLine("trace:hello" ;//可用于發(fā)布程序
Debug.WriteLine("debug:hello" ;//僅在調試模式下運行
Trace.WriteLine("trace:hello" ;//可用于發(fā)布程序
記得要使用Debug或Trace,需要引入相關類
C#代碼 收藏代碼
1. using System.Diagnostics;
using System.Diagnostics;
在IDE下查看輸入,視圖-輸出(view-output)
在IDE下,標準工具欄中,可進行Debug/Release模式的切換
其他
C#代碼 收藏代碼
1. Debug.WriteIf();
2. Debug.WriteLineIf();
3. Trace.WriteIf();
4. Trace.WriteLineIf();
Debug.WriteIf();
Debug.WriteLineIf();
Trace.WriteIf();
Trace.WriteLineIf();
插入點 (類似DeBug.WriteLine語句,但不需要改變代碼,為IDE自帶功能)
C#代碼 收藏代碼
1. 1。選中代碼行
2. 2。右鍵-斷點(BrackPoint)-插入跟蹤點(insert TracePonit)-彈出的窗口中輸入文本
3.
1。選中代碼行
2。右鍵-斷點(BrackPoint)-插入跟蹤點(insert TracePonit)-彈出的窗口中輸入文本
要查看所有斷點(breckPoint),可以在斷點窗口中查看 主菜單中: 調試-窗口-斷點 (Debug-window-breackPoint)
錯誤處理
try..catch....finally
好熟悉把,這個和java差不多,后面討 |
|