C#的winform商品信息管理程序

编写一个Windows应用程序,具体要求:

  1. 创建SqlServer数据库MyDb(使用集成身份验证方式访问数据库)

  2. 创建商品表"Products",Products表中的字段及类型如表1所示。建表成功后自行插入数据若干条。

  3. 创建一个"显示所有商品信息"窗体,如图1所示。窗体加载时即显示数据库表Products中所有数据。运行效果如图2所示。

表1 Products表字段说明

|-----------------|--------------|------|------|
| 字段名 | 数据类型 | 是否主键 | 含义 |
| ProductID | nvarchar(10) | 是 | 商品编号 |
| ProductName | nvarchar(30) | 否 | 商品名称 |
| ProductCategory | nvarchar(20) | 否 | 商品类别 |
| Price | numeric(7,2) | 否 | 商品单价 |

代码如下所示:

先在sql sever软件里创建表,如下图所示:

在vs软件里创建两个窗体,具体如下图:

代码如下:要连接数据库的字符串,如下:

private void Form1_Load(object sender, EventArgs e)

{

// 打开数据库连接

sqloperation.dbopen();

string sql = "select * from product";

// 绑定数据集到DataGridView控件

dataGridView1.DataSource = sqloperation.query(sql);

}

private void button2_Click(object sender, EventArgs e)

{

this.Close();

}

private void button1_Click(object sender, EventArgs e)

{

updateform addform = new updateform();

addform.ShowDialog();

this.Close();

}

添加窗体代码如下:

private void button1_Click(object sender, EventArgs e)

{

string id = textBox1.Text.Trim(); // 商品编号

string name = textBox2.Text.Trim();// 商品名称

string category = textBox3.Text.Trim();// 商品类别

string price = textBox4.Text.Trim();// 商品单价

string sql = $"insert into product values('{id}','{name}','{category}','{price}')";

update(sql);

this.Close();

Form1 info = new Form1();

info.ShowDialog();

}

//执行操作

public static string update(string sql)

{

try{

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=product;Integrated Security=True"); // 打开数据库连接

conn.Open();

SqlCommand cmd = new SqlCommand(sql, conn);

int result = cmd.ExecuteNonQuery();

conn.Close();

MessageBox.Show("商品添加成功");

return "success";

}

catch(Exception e)

{

MessageBox.Show(e.Message);

return e.ToString();

}

}

相关推荐
hez201028 分钟前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
RestCloud7 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud7 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence9 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
mudtools14 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
DemonAvenger16 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
唐青枫17 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud1 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net