SQL Server从入门到精通——C#与数据库

C#与数据库

一、 实验目的

1.掌握SQL中SQL语句的使用。

2.能够使用C#程序操作SQL数据库。

二、实验内容

在电子商务系统中,连接数据库ECS,将所有图书价格下降10%。

三、实验过程

  1. 导入数据库数据。点击视图,服务器资源管理器,右击数据连接,打开添加连接,服务器名输入LAPTOP-U364DCHI,在下方选择数据库名称。
  2. 窗体设计

3.输入代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace C_yushujuku

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

string connString = "Data Source= . ;Initial Catalog=studentcourse;integrated security=true";

SqlConnection connection = new SqlConnection(connString);

connection.Open();

string sql = "SELECT * FROM StudentMsg";

SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);

DataSet dataSet = new DataSet();

dataAdapter.Fill(dataSet);

dataGridView1.DataSource = dataSet.Tables0;

}

private void button2_Click(object sender, EventArgs e)

{

this.Close();

}

private void button3_Click_1(object sender, EventArgs e)

{

string connString = "Data Source= . ;Initial Catalog= ECS;integrated security=true";

SqlConnection connection = new SqlConnection(connString);

string sql = "update 商品 set 商品价格=商品价格*0.9"; //SQL语句

connection.Open();// 打开数据库连接

SqlCommand command = new SqlCommand(sql, connection); //创建 Command 对象

int count = command.ExecuteNonQuery();// 执行更新命令,返回值为更新的行数

if (count > 0)

{

string message = String.Format("更新成功,共有{0}本书降价", count);

MessageBox.Show(message,"更新成功", MessageBoxButtons.OK,

MessageBoxIcon.Information);

}

else

{

MessageBox.Show("没有可降价的图书", "无更新", MessageBoxButtons.OK,

MessageBoxIcon.Information);

}

}

}

}

  • 实验结果
相关推荐
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
彧azz3 小时前
算法设计与分析:贪心与动态规划
数据结构·学习·算法·贪心算法·动态规划
哈__4 小时前
KES-Operator重塑Kubernetes环境下的KES数据库集群管理
数据库·kubernetes·operator·kes
一条小小yu5 小时前
基于数据库唯一索引 + 过期时间的分布式锁
学习
传奇开心果编程5 小时前
【ArkUI 练中学】第15课:UI 界面设计与实战
学习·ui·华为·harmonyos
IvorySQL6 小时前
IvorySQL 5.6 发布:PG 18.6 内核升级,沙盒即开即用
数据库·人工智能·postgresql
CNSSIRD数据库7 小时前
中国企业环境信用数据库2003-2026
大数据·数据库·数据分析·论文笔记
传奇开心果编程7 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
fd32007 小时前
达梦数据库更新统计信息后仍走旧执行计划解决方案
数据库