欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    C#window窗体实现数据库连接.doc

    • 资源ID:23955152       资源大小:1.30MB        全文页数:38页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C#window窗体实现数据库连接.doc

    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:查询数据:成绩表: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 : Form 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("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 = 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" + " 终止尝试并生成错误之前所等待的时间: " + 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; /创建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("性别为女的客户的客户编号和姓名是:"); / 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 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 (result = 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(commandTextInsert, connection); cmdInsert.Parameters.AddWithValue("姓名", "周七"); cmdInsert.Parameters.AddWithValue("学号", 005); cmdInsert.Parameters.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("插入记录操作失败."); 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; 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 commandTextUpdate = "Update Region Set RegionDescription = 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 (result = 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 = 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" 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文件测试想要连接的数据库,能够得到连接数据库时所需要的计算机信息和数据库信息:-

    注意事项

    本文(C#window窗体实现数据库连接.doc)为本站会员(豆****)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开