C#連接數(shù)據(jù)庫及增刪查改操作_第1頁
C#連接數(shù)據(jù)庫及增刪查改操作_第2頁
C#連接數(shù)據(jù)庫及增刪查改操作_第3頁
C#連接數(shù)據(jù)庫及增刪查改操作_第4頁
C#連接數(shù)據(jù)庫及增刪查改操作_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、C#連接數(shù)據(jù)庫及增刪查改操作 設(shè)計(jì)好的C#界面:共3個(gè)窗體。數(shù)據(jù)庫:運(yùn)行后的界面:代碼:(Form1)public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) Form1_Load(sender, e); private void Form1_Load(object sender, EventArgs e) try SqlConnection conn = new SqlConnection(); s

2、tring str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from 基本信息表" cmd.Connection = conn; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet

3、 ds = new DataSet(); sda.Fill(ds, "基本信息表"); this.dataGridView1.DataSource = ds; this.dataGridView1.DataMember = "基本信息表" this.dataGridView1.AutoGenerateColumns = true; for (int i = 1; i < this.dataGridView1.ColumnCount; i+) this.dataGridView1.Columnsi.DefaultCellStyle.Selection

4、BackColor = Color.White; this.dataGridView1.Columnsi.DefaultCellStyle.SelectionForeColor = Color.Black; this.dataGridView1.Columnsi.ReadOnly = true; conn.Close(); catch(Exception ee) MessageBox.Show("錯(cuò)誤:"+ee.Message,"錯(cuò)誤"); private void button2_Click(object sender, EventArgs e) if

5、 (this.dataGridView1.SelectedCells=null) MessageBox.Show("請(qǐng)選擇要?jiǎng)h¦除的項(xiàng)!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else if (this.dataGridView1.CurrentCell.ColumnIndex = 0) string st = this.dataGridView11, this.dataGridView1.CurrentCell.RowIndex.Value.ToString(); SqlCo

6、nnection conn = new SqlConnection(); string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "delete from 基本信息表 where 姓名='" + st + "'"

7、cmd.Connection = conn; cmd.ExecuteNonQuery(); conn.Close(); private void button3_Click(object sender, EventArgs e) if (this.textBox1.Text.Trim() = "") MessageBox.Show("請(qǐng)輸入要查詢的姓名!", "提示"); else SqlConnection conn = new SqlConnection(); string str = "Data Source=ZWEI

8、QUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from 基本信息表 where 姓名='" + this.textBox1.Text.Trim() + "'" cmd.Connection = conn; SqlDataAdapter sda = new

9、 SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); this.dataGridView1.DataSource = ds; this.dataGridView1.DataMember = ds.Tables0.ToString(); this.dataGridView1.AutoGenerateColumns = true; conn.Close(); private void button4_Click(object sender, EventArgs e) Form2 f = new Form2(); f.Show

10、(); private void button5_Click(object sender, EventArgs e) Form3 f = new Form3(); f.id = Convert.ToInt32(this.dataGridView10, this.dataGridView1.CurrentCell.RowIndex.Value.ToString(); f.Show(); (Form2)public partial class Form2 : Form public Form2() InitializeComponent(); private void Form2_Load(obj

11、ect sender, EventArgs e) comboBox1.Items.Add("男"); comboBox1.Items.Add("女"); comboBox2.Items.Add("計(jì)算機(jī)11-1班"); comboBox2.Items.Add("計(jì)算機(jī)11-2班"); comboBox2.Items.Add("計(jì)算機(jī)11-3班"); comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox2.DropDownSt

12、yle = ComboBoxStyle.DropDownList; get_bh(); private void get_bh() SqlConnection conn = new SqlConnection(); string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText =

13、 "select * from 基本信息表" /cmd.CommandText = "select isnull(max(編號(hào)),0)+1 from 基本信息表" cmd.Connection = conn; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); int t = ds.Tables0.Rows.Count; int bh = t - 1; this.textBox1.Text = (Int32.Parse(ds.Tab

14、les0.Rowsbh"編號(hào)".ToString() + 1).ToString(); /this.textBox1.Text = cmd.ExecuteScalar().ToString(); conn.Close(); private void button1_Click(object sender, EventArgs e) if (textBox2.Text.Trim() = "" | textBox3.Text.Trim() = "" | comboBox1.Text.Trim() = "" | comb

15、oBox2.Text.Trim() = "") MessageBox.Show("請(qǐng)輸入完整信息!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else string str1 = textBox3.Text.Trim(); int l = str1.Length; for (int i = 0; i < l; i+) if (!char.IsNumber(str1i) MessageBox.Show("年齡輸入有誤!", "錯(cuò)誤&quo

16、t;, MessageBoxButtons.OK, MessageBoxIcon.Error); textBox3.SelectAll(); textBox3.Focus(); return; SqlConnection conn = new SqlConnection(); string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new

17、SqlCommand(); cmd.CommandText = "insert into 基本信息表(編號(hào),姓名,性別,年齡,班級(jí))values(編號(hào),姓名,性別,年齡,班級(jí))" cmd.Connection = conn; cmd.Parameters.Add("編號(hào)", SqlDbType.Int); cmd.Parameters.Add("姓名", SqlDbType.NVarChar, 50); cmd.Parameters.Add("性別", SqlDbType.NVarChar, 50); cmd.Pa

18、rameters.Add("年齡", SqlDbType.Int); cmd.Parameters.Add("班級(jí)", SqlDbType.NVarChar, 50); cmd.Parameters"編號(hào)".Value = textBox1.Text.Trim(); cmd.Parameters"姓名".Value = textBox2.Text.Trim(); cmd.Parameters"性別".Value = comboBox1.Text.Trim(); cmd.Parameters&qu

19、ot;年齡".Value = textBox3.Text.Trim(); cmd.Parameters"班級(jí)".Value = comboBox2.Text.Trim(); cmd.ExecuteNonQuery(); conn.Close(); this.Close(); private void button2_Click(object sender, EventArgs e) this.Close(); (Form3)public partial class Form3 : Form public Form3() InitializeComponent();

20、 public int id; private void Form3_Load(object sender, EventArgs e) textBox1.Text = id.ToString(); SqlConnection conn = new SqlConnection("Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; conn.Open(); cmd.

21、CommandText = "select * from 基本信息表 where 編號(hào)=" + id.ToString(); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables0.Rows.Count = 1) this.textBox2.Text = (ds.Tables0.Rows0"姓名".ToString(); this.textBox3.Text = (ds.Tables0.Rows0"

22、;性別".ToString(); this.textBox4.Text = (ds.Tables0.Rows0"年齡".ToString(); this.textBox5.Text = (ds.Tables0.Rows0"班級(jí)".ToString(); conn.Close(); private void button1_Click(object sender, EventArgs e) if (textBox2.Text.Trim() = "" | textBox3.Text.Trim() = "" |

23、 textBox4.Text.Trim() = "" | textBox5.Text.Trim() = "") MessageBox.Show("請(qǐng)輸入完整信息!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else string str1 = textBox4.Text.Trim(); int l = str1.Length; for (int i = 0; i < l; i+) if (!char.IsNumber(str1i) Message

24、Box.Show("年齡輸入有誤!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox4.SelectAll(); textBox4.Focus(); return; SqlConnection conn = new SqlConnection(); string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" conn.ConnectionString = str; conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "update 基本信息表 set 編號(hào)=編號(hào),姓名=姓名,性別=性別,年齡=年齡,班級(jí)=班級(jí) where 編號(hào)=編號(hào)" cmd.Connection = con

溫馨提示

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

評(píng)論

0/150

提交評(píng)論