C#資料庫操作小結

來源:互聯網
上載者:User

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中擷取資料庫連接字串
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(); //資料讀取
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);
//資料讀取
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);
//添加資料
DataRow newRow = dataTable.NewRow();
newRow["id"] = "tesr";
newRow["name"] = "111";
dataTable.Rows.Add(newRow);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到資料庫
}
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);
//修改資料
DataRow updateRow = dataTable.Rows[0];
updateRow["id"] = "update";
updateRow["name"] = "222";
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataTable); //更新到資料庫
}
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); //更新到資料庫
}
catch (SqlException ex)
{
throw ex;
}
}

5、DataSet類型
DataSet操作跟DataTabel操作基本是一樣的,只是DataSet可以儲存有多個表格,所以就多做介紹了
6、個人總結
個人感覺,用 SqlCommand比較靈活,而DataSet是實現ADO.NET斷開式串連的核心,比較安全

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.