C#數(shù)據(jù)庫操作小結(jié)
更新時間:2012年09月24日 20:14:11 作者:
每次做項目都會用到數(shù)據(jù)庫,對數(shù)據(jù)庫的操作都是糊里糊涂從書里找代碼用。通過昨天晚上與今天早上的努力,把數(shù)據(jù)庫的操作整理了一下,下面把整理結(jié)果做個小結(jié)
1、常用的T-Sql語句
查詢:SELECT * FROM tb_test WHERE ID='1' AND name='xia'
SELECT * FROM tb_test
插入:INSERT INTO tb_test VALUES('xia','123')
INSERT INTO tb_test(name) VALUES('xia')
更新:UPDATE tb_test SET password='234' WHERE ID='1'
刪除:DELETE FROM tb_test WHERE ID='1'
DELETE tb_test WHERE ID='1'
2、在vs2010中獲取數(shù)據(jù)庫連接字符串
string connectionString = Properties.Settings.Default.DatabaseTestConnectionString;
3、SqlCommand類型
查詢:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand command = new SqlCommand(selectStr, connection);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
label1.Text = "name:" + reader["name"].ToString(); //數(shù)據(jù)讀取
command.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
}
}
插入、修改、刪除:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand command = new SqlCommand(cmdStr, connection);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
}
}
4、DataTable類型,查詢、添加、修改、刪除
DataTable使用查詢、添加、刪除、修改時,需要用到SqlDataAdapter類
string selectStr = "SELECT * FROM tb_test2";
查詢:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//數(shù)據(jù)讀取
label1.Text = dataTable.Rows[0][0].ToString();
}
catch (SqlException ex)
{
throw ex;
}
}
添加:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//添加數(shù)據(jù)
DataRow newRow = dataTable.NewRow();
newRow["id"] = "tesr";
newRow["name"] = "111";
dataTable.Rows.Add(newRow);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
修改:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//修改數(shù)據(jù)
DataRow updateRow = dataTable.Rows[0];
updateRow["id"] = "update";
updateRow["name"] = "222";
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
刪除:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataTable.Rows[0].Delete(); //刪除記錄
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
5、DataSet類型
DataSet操作跟DataTabel操作基本是一樣的,只是DataSet可以儲存有多個表格,所以就多做介紹了
6、個人總結(jié)
個人感覺,用 SqlCommand比較靈活,而DataSet是實現(xiàn)ADO.NET斷開式連接的核心,比較安全
查詢:SELECT * FROM tb_test WHERE ID='1' AND name='xia'
SELECT * FROM tb_test
插入:INSERT INTO tb_test VALUES('xia','123')
INSERT INTO tb_test(name) VALUES('xia')
更新:UPDATE tb_test SET password='234' WHERE ID='1'
刪除:DELETE FROM tb_test WHERE ID='1'
DELETE tb_test WHERE ID='1'
2、在vs2010中獲取數(shù)據(jù)庫連接字符串
string connectionString = Properties.Settings.Default.DatabaseTestConnectionString;
3、SqlCommand類型
查詢:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand command = new SqlCommand(selectStr, connection);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
label1.Text = "name:" + reader["name"].ToString(); //數(shù)據(jù)讀取
command.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
}
}
插入、修改、刪除:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlCommand command = new SqlCommand(cmdStr, connection);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
}
}
4、DataTable類型,查詢、添加、修改、刪除
DataTable使用查詢、添加、刪除、修改時,需要用到SqlDataAdapter類
string selectStr = "SELECT * FROM tb_test2";
查詢:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//數(shù)據(jù)讀取
label1.Text = dataTable.Rows[0][0].ToString();
}
catch (SqlException ex)
{
throw ex;
}
}
添加:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//添加數(shù)據(jù)
DataRow newRow = dataTable.NewRow();
newRow["id"] = "tesr";
newRow["name"] = "111";
dataTable.Rows.Add(newRow);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
修改:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//修改數(shù)據(jù)
DataRow updateRow = dataTable.Rows[0];
updateRow["id"] = "update";
updateRow["name"] = "222";
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
刪除:
復制代碼 代碼如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataTable.Rows[0].Delete(); //刪除記錄
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到數(shù)據(jù)庫
}
catch (SqlException ex)
{
throw ex;
}
}
5、DataSet類型
DataSet操作跟DataTabel操作基本是一樣的,只是DataSet可以儲存有多個表格,所以就多做介紹了
6、個人總結(jié)
個人感覺,用 SqlCommand比較靈活,而DataSet是實現(xiàn)ADO.NET斷開式連接的核心,比較安全
您可能感興趣的文章:
- C#連接db2數(shù)據(jù)庫的實現(xiàn)方法
- 深入分析C#連接Oracle數(shù)據(jù)庫的連接字符串詳解
- C#保存圖片到數(shù)據(jù)庫并讀取顯示圖片的方法
- c#數(shù)據(jù)庫與TXT導入導出的實例
- C#訪問PostGreSQL數(shù)據(jù)庫的方法
- C#連接Oracle數(shù)據(jù)庫的實例方法
- C#操作數(shù)據(jù)庫總結(jié)(vs2005+sql2005)
- C#中使用SQLite數(shù)據(jù)庫的方法介紹
- c# 獲取數(shù)據(jù)庫中所有表名稱的方法
- 淺析C# web訪問mysql數(shù)據(jù)庫-整理歸納總結(jié)
相關文章
C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog
這篇文章介紹了C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#中的Task.WhenAll和Task.WhenAny方法介紹
這篇文章介紹了C#中的Task.WhenAll和Task.WhenAny方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04WPF使用DrawingContext實現(xiàn)繪制刻度條
這篇文章主要為大家詳細介紹了如何利用WPF DrawingContext實現(xiàn)繪制刻度條,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09WPF實現(xiàn)動畫效果(二)之From/To/By動畫
這篇文章介紹了WPF實現(xiàn)動畫效果之From/To/By動畫,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06