tft每日頭條

 > 科技

 > sql數據庫遠程連接配置

sql數據庫遠程連接配置

科技 更新时间:2025-02-09 14:13:45

sql數據庫遠程連接配置(C操作SQL數據庫)1

本篇文章主要講解一下使用C#語言來對SQL數據庫進行【添加數據】【查詢數據】【删除數據】【更新數據】相關操作

01

創建連接對象

1)引用命名空間

using System.Data.SqlClient;

2)連接創建的數據庫,在程序加載的時候進行連接

//存放數據庫數據源 string strMyConnectoion = "Data Source=DESKTOP-U6V69B6;Initial Catalog=2022_Karl;Integrated Security=True"; SqlConnection myConnection;//數據庫連接對象 private void Form1_Load(object sender, EventArgs e) { //創建數據庫連接對象 myConnection = new SqlConnection(strMyConnectoion); }

02

數據寫入

在相應的輸入欄裡輸入數據,點擊【添加數據】即可将數據添加到表内

sql數據庫遠程連接配置(C操作SQL數據庫)2

以下為源代碼:

private void btnAdd_Click(object sender, EventArgs e) { bool res = false; try { //打開數據庫 myConnection.Open(); //實例化命令對象 SqlCommand myCommand = new SqlCommand(); //把要操作的數據庫傳過來 myCommand.Connection = myConnection; //操作類型為文本類型 myCommand.CommandType = CommandType.Text; // myCommand.CommandText = @"insert into studentList(學号,姓名,年齡,性别,地址) values(@學号,@姓名,@年齡,@性别,@地址)"; myCommand.Parameters.Add (new SqlParameter("@學号", textBox_ID.Text)); myCommand.Parameters.Add (new SqlParameter("@姓名", textBox_Name.Text)); myCommand.Parameters.Add (new SqlParameter("@年齡", textBox_Age.Text)); myCommand.Parameters.Add (new SqlParameter("@性别", textBox_Sex.Text)); myCommand.Parameters.Add (new SqlParameter("@地址", textBox_Addr.Text)); //提交數據 myCommand.ExecuteNonQuery(); //關閉數據庫 myConnection.Close(); res = true; } catch (Exception ex) { MessageBox.Show(ex.Tostring()); myConnection.Close(); } if (res) { MessageBox.Show("添加成功"); } }

03

數據查詢

點擊【查詢數據】,可以看到表内數據被成功顯示出來了

sql數據庫遠程連接配置(C操作SQL數據庫)3

以下為源代碼:

private void btnFind_Click(object sender, EventArgs e) { //select * from 表名 查詢整個表 //select 條件 from 表名 按條件查詢表格 try { //打開數據庫 myConnection.Open(); //實例化命令對象 SqlCommand myCommand = new SqlCommand(); //把要操作的數據庫傳過來 myCommand.Connection = myConnection; //操作類型為文本類型 myCommand.CommandType = CommandType.Text; //命令格式,代表查詢全部 myCommand.CommandText = @"select * from studentList"; //創建DataAdapter對象 SqlDataAdapter sda = new SqlDataAdapter(myCommand); //創建DataSet對象 DataSet ds = new DataSet(); //将studentList這張表填充到DataSet數據集中 sda.Fill(ds, "studentList"); //将studentList這張表顯示到控件上 dataGridView1.DataSource = ds.Tables["studentList"].DefaultView; //關閉數據庫 myConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); myConnection.Close(); } }

04

按條件查詢

在按姓名查詢欄輸入karl,點擊【按姓名查詢】可以看到符合條件的數據被篩選出來了

sql數據庫遠程連接配置(C操作SQL數據庫)4

以下為源代碼:

private void btnCdtFind_Click(object sender, EventArgs e) { //select * from 表名 -查詢整個表 //select 列名 from 表名 where 列 運算符 值 -按條件查詢表格 try { //打開數據庫 myConnection.Open(); //實例化命令對象 SqlCommand myCommand = new SqlCommand(); //把要操作的數據庫傳過來 myCommand.Connection = myConnection; //操作類型為文本類型 myCommand.CommandType = CommandType.Text; //命令格式,代表按姓名查詢 myCommand.CommandText = @"select * from studentList where 姓名 =@姓名"; myCommand.Parameters.Add(new SqlParameter("@姓名", textBox_Cdt.Text)); //開始查詢 int res =Convert.ToInt32(myCommand.ExecuteScalar()); //如果查詢不到,報錯 if (res == 0) { throw new Exception("查無此人"); } //創建DataAdapter對象 SqlDataAdapter sda = new SqlDataAdapter(myCommand); //創建DataSet對象 DataSet ds = new DataSet(); //将studentList這張表填充到DataSet數據集中 sda.Fill(ds, "studentList"); //将studentList這張表顯示到控件上 dataGridView1.DataSource = ds.Tables["studentList"].DefaultView; //關閉數據庫 myConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); myConnection.Close(); } }

05

删除數據

1)點擊【查詢數據】查看表内數據

sql數據庫遠程連接配置(C操作SQL數據庫)5

2)在按姓名删除欄輸入karl,點擊【按姓名删除】在彈出的窗口點擊【确定】

sql數據庫遠程連接配置(C操作SQL數據庫)6

3)再次點擊【查詢數據】可以發現姓名為karl的數據已經被删除了

sql數據庫遠程連接配置(C操作SQL數據庫)7

以下為源代碼:

private void btn_Del_Click(object sender, EventArgs e) { //删除語法 //delete from 表名 where 條件 try { //打開數據庫 myConnection.Open(); //實例化命令對象 SqlCommand myCommand = new SqlCommand(); //把要操作的數據庫傳過來 myCommand.Connection = myConnection; //操作類型為文本類型 myCommand.CommandType = CommandType.Text; //命令格式,代表按姓名查詢 myCommand.CommandText = @"delete from studentList where 姓名 =@姓名"; myCommand.Parameters.Add(new SqlParameter("@姓名", textBox_Del.Text)); //開始查詢 int res = Convert.ToInt32(myCommand.ExecuteNonQuery()); //如果查詢不到,報錯 if (res == 0) { throw new Exception("查無此人"); } MessageBox.Show("删除成功"); myConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); myConnection.Close(); } }

06

将修改後的值保存到數據庫

1)原來的性别欄下面的數據是男

sql數據庫遠程連接配置(C操作SQL數據庫)8

2)将性别由男改為女,按回車,在彈出的提示對話框中點擊【确定】

sql數據庫遠程連接配置(C操作SQL數據庫)9

3)點擊【确定】

sql數據庫遠程連接配置(C操作SQL數據庫)10

4)點擊【查詢數據】,可以看到性别欄的原來的男被改成了女

sql數據庫遠程連接配置(C操作SQL數據庫)11

以下為源代碼:

//定義一個object類型的變量用來存儲修改之前的數據 object cellTempValue = null; //修改之前的數據 private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { //将編輯的之前的數據存到cellTempValue變量中 cellTempValue= this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; } //修改之後的數據 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { //更新語法 //update 表名 set 列名=值 where 條件 //如果修改前的值與修改後的值相等,直接返回不做任何操作 if (object.Equals(cellTempValue, this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)) { return; } //如果選擇取消,将修改後的值還原 if (MessageBox.Show("是否确定修改,并更新到數據庫", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = cellTempValue; return; } //如果選擇确認,将修改後的值更新到數據庫 try { //打開數據庫 myConnection.Open(); //實例化命令對象 SqlCommand myCommand = new SqlCommand(); //把要操作的數據庫傳過來 myCommand.Connection = myConnection; //操作類型為文本類型 myCommand.CommandType = CommandType.Text; //命令格式 string strSql = String.Format( "update studentList set {0}='{1}' where 學号='{2}'", this.dataGridView1.Columns[e.ColumnIndex].HeaderText,//當前選擇的列名 this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value,//選中單元格修改後的值 this.dataGridView1.Rows[e.RowIndex].Cells[0].Value//選中單元格修改前的值 ) ; //命令格式 myCommand.CommandText = strSql; //語句執行 int res = myCommand.ExecuteNonQuery(); if (res == 0) { throw new Exception("修改失敗"); } else { MessageBox.Show("修改成功"); } myConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); myConnection.Close(); } }

sql數據庫遠程連接配置(C操作SQL數據庫)12

sql數據庫遠程連接配置(C操作SQL數據庫)13

苦逼的自動化同胞們,加油!加油!加油!你離成功就差點個贊了,^_^

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved