版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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.ComponentModel;usin
2、g 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 姓名,语文 FROM 成绩表 W
3、HERE 学号= 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)
4、 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 = ConnectionState.Open) textBox2.Text = "数据库连接成功,SqlConnection信息如下:" +
5、"rn" + " 连接状态: " + connection.State + "rn" + " SQL Server实例的名称: " + connection.DataSource + "rn" + " 数据库名称: " + connection.Database + "rn" + " SQL Server 版本: " + connection.ServerVersion + "rn" + " 数据库客户端I
6、d: " + connection.WorkstationId + "rn" + " 终止尝试并生成错误之前所等待的时间: " + connection.ConnectionTimeout + " 秒" + "rn" + " 网络数据包大小: " + connection.PacketSize + " 字节" + "rn" private void button2_Click_1(object sender, EventArgs e) textB
7、ox2.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.AddW
8、ithValue("001", textBox3.Text); SqlDataReader reader = cmdQuery.ExecuteReader(); textBox2.Text = "学号为001的学生的姓名和语文分数是:" + "rn" textBox2.Text = "姓名 tt 语文" + "rn" /Console.WriteLine("性别为女的客户的客户编号和姓名是:"); / Console.WriteLine("客户编号 t 姓名&quo
9、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 sender, E
10、ventArgs 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 (re
11、sult = 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;
12、SqlCommand cmdInsert = new SqlCommand(commandTextInsert, connection); cmdInsert.Parameters.AddWithValue("姓名", "周七"); cmdInsert.Parameters.AddWithValue("学号", 005); cmdInsert.Parameters.AddWithValue("语文", 105); cmdInsert.Parameters.AddWithValue("数学", 1
13、15); cmdInsert.Parameters.AddWithValue("英语", 124); / 执行SqlCommand命令并检查结果 int result = cmdInsert.ExecuteNonQuery(); if (result = 1) textBox2.Text = "插入记录操作成功." /Console.WriteLine("插入记录操作成功."); else textBox2.Text = "插入记录操作失败." / Console.WriteLine("插入记录操作失败.
14、"); 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:N
15、ORTHWND.MDF; Integrated 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 commandTextU
16、pdate = "Update Region Set RegionDescription = name WHERE RegionID = id" / 创建SqlCommand命令 SqlCommand cmdUpdate = new SqlCommand("Update 成绩表 Set 语文=语文 WHERE 学号 =学号", connection1); cmdUpdate.Parameters.AddWithValue("学号",002); cmdUpdate.Parameters.AddWithValue("语文&quo
17、t;,131); / 执行SqlCommand命令并检查结果 int result = cmdUpdate.ExecuteNonQuery(); if (result = 1) textBox2.Text = "更新记录操作成功." /Console.WriteLine("更新记录操作成功."); else textBox2.Text = "更新记录操作失败." /Console.WriteLine("更新记录操作失败."); private void button6_Click_1(object sender,
18、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" /Console.WriteLine("Products表中最贵的10个商品的信息:"); / Console.WriteLine(" 产品名称 ttt单价"); / 通过循环列表显示查询结果集 while (reader.Read() string rowInfo = reader0.ToString().PadRight(30) + "t" + reader1 + "rn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 63296-3:2025 EN Portable multimedia equipment - Determination of battery duration - Part 3: Wearable powered loudspeaker equipment
- 2025年二级建造师《建筑工程管理与实务》真题试题及参考答案
- 冲件压件行业深度研究报告
- 串接式双孔终端用户盒行业深度研究报告
- 中国手机机壳的项目投资可行性研究报告
- 青纶混纺机织衫行业深度研究报告
- 中国水轮机调制器项目投资可行性研究报告
- 平面活性炭防毒口罩行业深度研究报告
- 2026年牛街集镇供水工程可研设计报告
- 双虎线行业深度研究报告
- 病理医生进修病理诊断汇报
- 2025年入党积极分子考试题库及答案(全国)
- 单招考试宣传课件
- 2024-2025学年安徽合肥七年级上册数学期中试卷及答案
- 塔罗教学课件
- 产程中产妇的饮食管理
- 2025年陕西邮政校招笔试题及答案
- 三务公开培训
- 2025年衡水驾校考试危运证考试
- 肺动脉高压讲课件
- 麻醉科进修结业汇报大纲
评论
0/150
提交评论