C#window窗體實現數據庫連接(共18頁)_第1頁
C#window窗體實現數據庫連接(共18頁)_第2頁
C#window窗體實現數據庫連接(共18頁)_第3頁
C#window窗體實現數據庫連接(共18頁)_第4頁
C#window窗體實現數據庫連接(共18頁)_第5頁
已閱讀5頁,還剩13頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優質文檔-傾情為你奉上C#程序進行數據庫連接,以及數據庫的更新,插入,修改,查詢等維護功能。步驟:1:創建windows窗體應用程序如下圖放置所需要的控件:添加1個tabpage1控件,1個Lable控件,6個Button控件,2個textbox控件再按下列表格設置各控件的屬性更改他們的名字;3:程序運行結果圖;1:數據庫連接:2:查詢數據:成績表:3:刪除數據:刪除前成績表:刪除后成績表:4:插入數據:插入前成績表:5:更新數據庫:更新前成績表:4:程序源代碼using System;using System.Collections.Generic;using System.Compon

2、entModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace DataBase public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) comboBox1.Items.Add(SELECT 姓名,語文 FRO

3、M 成績表 WHERE 學號= 001); comboBox1.Items.Add(DELETE FROM 成績表 WHERE 數學 = 115); comboBox1.Items.Add(INSERT INTO 成績表(姓名,學號,語文,數學,英語) VALUES(姓名,學號,語文,數學,英語); comboBox1.Items.Add(Update 成績表 Set 語文= 131 WHERE 學號 =002); private void button1_Click(object sender, EventArgs e) string connectonString = Integrated

4、 Security=SSPI;Persist Security Info=False; Initial Catalog=sql;Data Source=I-PC; connection = new SqlConnection(connectonString); connection.Open(); if (connection.State = ConnectionState.Open) textBox2.Text = 數據庫連接成功,SqlConnection信息如下: + rn + 連接狀態: + connection.State + rn + SQL Server實例的名稱: + conn

5、ection.DataSource + rn + 數據庫名稱: + connection.Database + rn + SQL Server 版本: + connection.ServerVersion + rn + 數據庫客戶端Id: + connection.WorkstationId + rn + 終止嘗試并生成錯誤之前所等待的時間: + connection.ConnectionTimeout + 秒 + rn + 網絡數據包大小: + connection.PacketSize + 字節 + rn; private void button2_Click_1(object sende

6、r, EventArgs e) textBox2.Text = null; String commandTextQuery = comboBox1.Text; / String commandTextQuery = SELECT * FROM Region where RegionID=zhangsan+textBox1.Text; /創建SqlCommand命令 SqlCommand cmdQuery = new SqlCommand(commandTextQuery, connection); /執行SqlCommand命令并返回結果 cmdQuery.Parameters.AddWith

7、Value(001, textBox3.Text); SqlDataReader reader = cmdQuery.ExecuteReader(); textBox2.Text = 學號為001的學生的姓名和語文分數是: + rn; textBox2.Text = 姓名 tt 語文 + rn; /Console.WriteLine(性別為女的客戶的客戶編號和姓名是:); / Console.WriteLine(客戶編號 t 姓名); /通過循環列表顯示查詢結果集 while (reader.Read() string rowInfo = reader0 + t + reader1 + rn;

8、 textBox2.Text = textBox2.Text + rowInfo; /Console.WriteLine( 0 tt 1, reader0, reader1); /關閉查詢結果集 reader.Close(); /Console.ReadLine(); private void button5_Click_1(object sender, EventArgs e) textBox2.Text = null; String commandTextDelete = comboBox1.Text; / 創建SqlCommand命令 SqlCommand cmdDelete = new

9、 SqlCommand(commandTextDelete, connection); cmdDelete.Parameters.AddWithValue(115, textBox3.Text); /執行SqlCommand命令并檢查結果 int result = cmdDelete.ExecuteNonQuery(); if (result = 1) textBox2.Text = 刪除記錄操作成功.; /Console.WriteLine(刪除記錄操作成功.); else textBox2.Text = 刪除記錄操作失敗.; / Console.WriteLine(刪除記錄操作失敗.);

10、private void button3_Click_1(object sender, EventArgs e) textBox2.Text = null; String commandTextInsert = comboBox1.Text; SqlCommand cmdInsert = new SqlCommand(commandTextInsert, connection); cmdInsert.Parameters.AddWithValue(姓名, 周七); cmdInsert.Parameters.AddWithValue(學號, 005); cmdInsert.Parameters.

11、AddWithValue(語文, 105); cmdInsert.Parameters.AddWithValue(數學, 115); cmdInsert.Parameters.AddWithValue(英語, 124); / 執行SqlCommand命令并檢查結果 int result = cmdInsert.ExecuteNonQuery(); if (result = 1) textBox2.Text = 插入記錄操作成功.; /Console.WriteLine(插入記錄操作成功.); else textBox2.Text = 插入記錄操作失敗.; / Console.WriteLine

12、(插入記錄操作失敗.); private void button4_Click_1(object sender, EventArgs e) textBox2.Text = null; String connectonString = Server=.SQLEXPRESS;Integrated Security=SSPI;Persist Security Info=False; Initial Catalog=sql;Data Source=I-PC; / String connectonString = Server=.SQLEXPRESS;DataBase=C:NORTHWND.MDF; I

13、ntegrated Security = SSPI; / String connectonString = Data Source = .SQLEXPRESS; Initial Catalog= c:NORTHWND.MDF; Integrated Security = True;, System.Data.SqlClient.SqlConnection connection1= new SqlConnection(connectonString); connection1.Open(); /String commandTextUpdate = Update Region Set Region

14、Description = name WHERE RegionID = id; / 創建SqlCommand命令 SqlCommand cmdUpdate = new SqlCommand(Update 成績表 Set 語文=語文 WHERE 學號 =學號, connection1); cmdUpdate.Parameters.AddWithValue(學號,002); cmdUpdate.Parameters.AddWithValue(語文,131); / 執行SqlCommand命令并檢查結果 int result = cmdUpdate.ExecuteNonQuery(); if (re

15、sult = 1) textBox2.Text = 更新記錄操作成功.; /Console.WriteLine(更新記錄操作成功.); else textBox2.Text = 更新記錄操作失敗.; /Console.WriteLine(更新記錄操作失敗.); private void button6_Click_1(object sender, EventArgs e) SqlCommand cmdQuery = new SqlCommand(Ten Most Expensive Products, connection); cmdQuery.CommandType = CommandTyp

16、e.StoredProcedure; / 執行SqlCommand命令并返回結果 SqlDataReader reader = cmdQuery.ExecuteReader(); textBox2.Text = Products表中最貴的10個商品的信息: + rn; textBox2.Text = 產品名稱 ttt單價 + rn; /Console.WriteLine(Products表中最貴的10個商品的信息:); / Console.WriteLine( 產品名稱 ttt單價); / 通過循環列表顯示查詢結果集 while (reader.Read() string rowInfo = reader0.ToString().PadRight(30) + t + reader1 + rn; textBox2.Text +=rowInfo; /關閉查詢結

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論