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);

}

}

}

}

  • 实验结果
相关推荐
吃糖的小孩1 天前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
笃行3502 天前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行3502 天前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库
笃行3502 天前
金仓数据库逻辑备份实战:从全库导出到 Schema 替换的完整闭环
数据库
SelectDB3 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
这个DBA有点耶3 天前
GROUP BY优化全解:如何写出既不丢数据又飞快的分组查询
数据库·mysql·架构
掉头发的王富贵3 天前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
Nturmoils3 天前
WHERE 条件别凭习惯写,常用查询先跑一遍
数据库
Databend4 天前
在 AWS 中国峰会逛了一天,我在 Databend 展台看到了 Agent 数据基础设施的新思路
数据库·人工智能·agent