这篇文章是一个项目的部分代码,一个项目太长不便阅读,所以分开来。
c# 基于数据库的商品信息管理系统实例(含登录和操作两个界面)
原文在这儿,看的话从这儿开始看:http://blog.csdn.net/tingzhiyi/article/details/49072907
源代码在这儿可以下载:http://download.csdn.net/detail/tingzhiyi/9173473
数据操作效果图:
数据操作程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADO
{
public partial class operation : Form
{
public operation()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
#region 显示总体分类
//确定数据库连接字符串
string constr = "server=Flz;database=product;Integrated Security=True;";
//实例化数据库连接对象
SqlConnection conn = new SqlConnection(constr);
//打开数据库连接
conn.Open();
//实例化SqlCommand 对象(该对象主要用来执行SQL命令)
SqlCommand comm = new SqlCommand();
comm.CommandText = "select id,name from classify";
comm.CommandType = CommandType.Text;
comm.Connection = conn;
//申明一个SqlDataReader(数据流)对象,并将comm执行后的结果交给他
SqlDataReader sdr = comm.ExecuteReader();
while (sdr.Read())
{
comboBox1.Items.Add(sdr[0]+"-->"+sdr["name"]);
}
sdr.Close();
conn.Close();
#endregion
#region 左下角显示商品种类
using (SqlConnection conn2 = new SqlConnection(constr))
{
conn2.Open();
SqlCommand comm2 = new SqlCommand("select count(*) from product",conn2);
string sum = comm2.ExecuteScalar().ToString();
label2.Text = string.Format("共计{0}种商品",sum);
}
#endregion
}
#region 载入分类
private void button1_Click(object sender, EventArgs e)
{
string constr = "server=Flz;database=product;Integrated Security=True;";
SqlConnection conn = new SqlConnection(constr);
conn.Open();
SqlCommand comm = new SqlCommand();
//获取分类id
int i = comboBox1.Text.IndexOf("-->");//获取字符串中“-->”所在位置索引
string id = comboBox1.Text.Substring(0,i);//只获取-->之前的字符
if (Convert.ToInt32(id)==0)
{
MessageBox.Show("请先选择分类!");
}
comm.CommandText = "select *from product where c_id="+id;
comm.CommandType = CommandType.Text;
comm.Connection = conn;
SqlDataReader sdr = comm.ExecuteReader();
listBox1.Items.Clear();
while (sdr.Read())
{
listBox1.Items.Add( sdr["id"]+":"+sdr["name"]);
}
sdr.Close();
conn.Close();
}
#endregion
#region 显示名称、价格、库存详细信息
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
button2.Text = "修改";
string constr = "server=Flz;database=product;Integrated Security=True;";
using (SqlConnection conn=new SqlConnection(constr))
{
int index = comboBox1.Text.IndexOf("-->");//获取字符串中“-->”所在位置索引
string id = comboBox1.Text.Substring(0, index);//只获取-->之前的字符
int index1 = listBox1.Text.IndexOf(":"); //获取字符串中:所在位置索引
string id1 = listBox1.Text.Substring(0,index); //只获取:之前的字符
string sql = "select *from product where (c_id=' " + id + "' )and (id='" + id1+ "')";
SqlCommand comm = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader sdr = comm.ExecuteReader();
while (sdr.Read())
{
tb_name.Text = sdr["name"].ToString();
tb_price.Text = sdr["price"].ToString();
tb_number.Text = sdr["number"].ToString();
}
sdr.Close();
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
switch (((Button)sender).Text)
{
#region 添加商品
case "添加":
string constr = "server=Flz;database=product;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(constr))
{
int i = comboBox1.Text.IndexOf("-->");//获取字符串中“-->”所在位置索引
string index = comboBox1.Text.Substring(0, i);//只获取-->之前的字符
string sql1="select max(id) from product where c_id = '"+index+"';";
conn.Open();
SqlCommand comm1 = new SqlCommand(sql1, conn);
int idmax = Convert.ToInt32(comm1.ExecuteScalar()) + 1;
conn.Close();
if (int.Parse(index) > 0)
{
if (tb_name.Text == string.Empty) MessageBox.Show("请输入商品名称");
else if (tb_price.Text == string.Empty) MessageBox.Show("请输入商品价格");
else if (tb_number.Text == string.Empty) MessageBox.Show("请输入商品库存");
else
{
string sql = "insert into product(id,name,price,number,c_id) values('" + idmax + "','" + tb_name.Text + "','" + tb_price.Text + "','" + tb_number.Text + "','" + index + "') ";
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
int j = comm.ExecuteNonQuery();
if (j > 0) MessageBox.Show("成功添加" + j.ToString() + "种商品的数据!");
else MessageBox.Show("真尴尬,一行都没添加上!");
}
}
else MessageBox.Show("先选择一个分类再添加,OK ???");
} break;
#endregion
#region 修改商品
case "修改":
string constr1 = "server=Flz;database=product;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(constr1))
{
int index = comboBox1.Text.IndexOf("-->");//获取字符串中“-->”所在位置索引
string c_id = comboBox1.Text.Substring(0, index);//只获取-->之前的字符
int index1 = listBox1.Text.IndexOf(":"); //获取字符串中:所在位置索引
string id = listBox1.Text.Substring(0, index); //只获取:之前的字符 where id=" + id and c_id=" + c_id + "
string sql = "update product set name='" + tb_name.Text + "',price=" + tb_price.Text + ",number=" + tb_number.Text +" where (c_id=' " + c_id + "' )and (id='" + id+ "')" ;
SqlCommand comm = new SqlCommand(sql,conn);
conn.Open();
int i = comm.ExecuteNonQuery();
if (i>0) MessageBox.Show("成功修改" + i.ToString() + "个商品的数据!");
else MessageBox.Show("真尴尬,一行都没修改好!");
conn.Close();
}
break;
#endregion
}
}
#region 删除商品
private void button3_Click(object sender, EventArgs e)
{
string constr = "server=Flz;database=product;Integrated Security =True;";
using(SqlConnection conn=new SqlConnection(constr) )
{
//获取当前选择行的id
int index = listBox1.Text.IndexOf(":");
string id = listBox1.Text.Substring(0,index);
int index1 = comboBox1.Text.IndexOf("-->");
string c_id = comboBox1.Text.Substring(0,index1);
if (MessageBox.Show("确定要删除此项吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk)==DialogResult.OK)
{
string sql = "delete from product where (id='" + id + "') and (c_id='" + c_id + "')";
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
int i = comm.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("成功删除" + i.ToString() + "行数据!");
tb_name.Text = string.Empty;
tb_price.Text = string.Empty;
tb_number.Text = string.Empty;
button2.Text = "添加";
}
else MessageBox.Show("删除失败!");
}
}
}
#endregion
#region 关闭窗口后退出程序
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
#endregion
#region 退出程序
private void bt_exit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
Application.Exit();
}
}
#endregion
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
tb_name.Text = string.Empty;
tb_price.Text = string.Empty;
tb_number.Text = string.Empty;
button2.Text = "添加";
}
}
}