《C#window窗体实现数据库连接.doc》由会员分享,可在线阅读,更多相关《C#window窗体实现数据库连接.doc(38页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC#window窗体实现数据库连接面向程序设计实验报告三C#程序进行数据库连接,以及数据库的更新,插入,修改,查询等维护功能。步骤:1:创建windows窗体应用程序如下图放置所需要的控件:添加1个tabpage1控件,1个Lable控件,6个Button控件,2个textbox控件再按下列表格设置各控件的属性更改他们的名字;3:程序运行结果图;1:数据库连接:2:查
2、询数据:成绩表:3:删除数据:删除前成绩表:删除后成绩表:4:插入数据:插入前成绩表:5:更新数据库:更新前成绩表:4:程序源代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace DataBase public partial class Form1 : F
3、orm public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) comboBox1.Items.Add(SELECT 姓名,语文 FROM 成绩表 WHERE 学号= 001); comboBox1.Items.Add(DELETE FROM 成绩表 WHERE 数学 = 115); comboBox1.Items.Add(INSERT INTO 成绩表(姓名,学号,语文,数学,英语) VALUES(姓名,学号,语文,数学,英语); comboBox1.Items.Add
4、(Update 成绩表 Set 语文= 131 WHERE 学号 =002); private void button1_Click(object sender, EventArgs e) string connectonString = Integrated Security=SSPI;Persist Security Info=False; Initial Catalog=sql;Data Source=I-PC; connection = new SqlConnection(connectonString); connection.Open(); if (connection.State
5、 = ConnectionState.Open) textBox2.Text = 数据库连接成功,SqlConnection信息如下: + rn + 连接状态: + connection.State + rn + SQL Server实例的名称: + connection.DataSource + rn + 数据库名称: + connection.Database + rn + SQL Server 版本: + connection.ServerVersion + rn + 数据库客户端Id: + connection.WorkstationId + rn + 终止尝试并生成错误之前所等待的时
6、间: + connection.ConnectionTimeout + 秒 + rn + 网络数据包大小: + connection.PacketSize + 字节 + rn; private void button2_Click_1(object sender, EventArgs e) textBox2.Text = null; String commandTextQuery = comboBox1.Text; / String commandTextQuery = SELECT * FROM Region where RegionID=zhangsan+textBox1.Text; /创
7、建SqlCommand命令 SqlCommand cmdQuery = new SqlCommand(commandTextQuery, connection); /执行SqlCommand命令并返回结果 cmdQuery.Parameters.AddWithValue(001, textBox3.Text); SqlDataReader reader = cmdQuery.ExecuteReader(); textBox2.Text = 学号为001的学生的姓名和语文分数是: + rn; textBox2.Text = 姓名 tt 语文 + rn; /Console.WriteLine(性别
8、为女的客户的客户编号和姓名是:); / Console.WriteLine(客户编号 t 姓名); /通过循环列表显示查询结果集 while (reader.Read() string rowInfo = reader0 + t + reader1 + rn; textBox2.Text = textBox2.Text + rowInfo; /Console.WriteLine( 0 tt 1, reader0, reader1); /关闭查询结果集 reader.Close(); /Console.ReadLine(); private void button5_Click_1(object
9、 sender, EventArgs e) textBox2.Text = null; String commandTextDelete = comboBox1.Text; / 创建SqlCommand命令 SqlCommand cmdDelete = new SqlCommand(commandTextDelete, connection); cmdDelete.Parameters.AddWithValue(115, textBox3.Text); /执行SqlCommand命令并检查结果 int result = cmdDelete.ExecuteNonQuery(); if (resu
10、lt = 1) textBox2.Text = 删除记录操作成功.; /Console.WriteLine(删除记录操作成功.); else textBox2.Text = 删除记录操作失败.; / Console.WriteLine(删除记录操作失败.); private void button3_Click_1(object sender, EventArgs e) textBox2.Text = null; String commandTextInsert = comboBox1.Text; SqlCommand cmdInsert = new SqlCommand(commandTex
11、tInsert, connection); cmdInsert.Parameters.AddWithValue(姓名, 周七); cmdInsert.Parameters.AddWithValue(学号, 005); cmdInsert.Parameters.AddWithValue(语文, 105); cmdInsert.Parameters.AddWithValue(数学, 115); cmdInsert.Parameters.AddWithValue(英语, 124); / 执行SqlCommand命令并检查结果 int result = cmdInsert.ExecuteNonQuer
12、y(); if (result = 1) textBox2.Text = 插入记录操作成功.; /Console.WriteLine(插入记录操作成功.); else textBox2.Text = 插入记录操作失败.; / Console.WriteLine(插入记录操作失败.); private void button4_Click_1(object sender, EventArgs e) textBox2.Text = null; String connectonString = Server=.SQLEXPRESS;Integrated Security=SSPI;Persist S
13、ecurity Info=False; Initial Catalog=sql;Data Source=I-PC; / String connectonString = Server=.SQLEXPRESS;DataBase=C:NORTHWND.MDF; Integrated Security = SSPI; / String connectonString = Data Source = .SQLEXPRESS; Initial Catalog= c:NORTHWND.MDF; Integrated Security = True;, System.Data.SqlClient.SqlCo
14、nnection connection1= new SqlConnection(connectonString); connection1.Open(); /String commandTextUpdate = Update Region Set RegionDescription = name WHERE RegionID = id; / 创建SqlCommand命令 SqlCommand cmdUpdate = new SqlCommand(Update 成绩表 Set 语文=语文 WHERE 学号 =学号, connection1); cmdUpdate.Parameters.AddWi
15、thValue(学号,002); cmdUpdate.Parameters.AddWithValue(语文,131); / 执行SqlCommand命令并检查结果 int result = cmdUpdate.ExecuteNonQuery(); if (result = 1) textBox2.Text = 更新记录操作成功.; /Console.WriteLine(更新记录操作成功.); else textBox2.Text = 更新记录操作失败.; /Console.WriteLine(更新记录操作失败.); private void button6_Click_1(object sen
16、der, EventArgs e) SqlCommand cmdQuery = new SqlCommand(Ten Most Expensive Products, connection); cmdQuery.CommandType = CommandType.StoredProcedure; / 执行SqlCommand命令并返回结果 SqlDataReader reader = cmdQuery.ExecuteReader(); textBox2.Text = Products表中最贵的10个商品的信息: + rn; textBox2.Text = 产品名称 ttt单价 + rn; /C
17、onsole.WriteLine(Products表中最贵的10个商品的信息:); / Console.WriteLine( 产品名称 ttt单价); / 通过循环列表显示查询结果集 while (reader.Read() string rowInfo = reader0.ToString().PadRight(30) + t + reader1 + rn; textBox2.Text +=rowInfo; /关闭查询结果集 reader.Close(); private void button8_Click(object sender, EventArgs e) connection.Close(); textBox2.Text = 数据库已关闭!; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 数据库连接有一个小技巧:命名一个udl文件测试想要连接的数据库,能够得到连接数据库时所需要的计算机信息和数据库信息:-