📄 store.cs
字号:
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 login
{
public partial class store : Form
{
SqlDataAdapter sqlDataAdapter1;
//存取数据库的主要类
SqlCommand sqlCommand1;
//SQL语句处理的类
SqlConnection sqlConnection1;
// 表示是否处于插入新记录的状态
DataSet dataSet1;
// 获取所有客户的ID
private void GetCustomID()
{
SqlDataReader sdr;
sqlConnection1.Open(); // 打开连接
sdr = sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
cbxID.Items.Clear();
while (sdr.Read())
{
// 把客户ID插入到组合框控件中
cbxID.Items.Add(sdr.GetValue(0));
}
sdr.Close(); // 关闭SqlDataReader对象和数据库连接
cbxID.SelectedIndex = 0;
}
public store()
{
InitializeComponent();
// Windows 安全登录机制
String sConnString = "Data Source=localhost;Initial Catalog=WZGL_20052070;Integrated Security=True";
//SQL语句
String sSQL = "SELECT * FROM room";
//创建一个数据库连接对象
sqlConnection1 = new SqlConnection(sConnString);
sqlCommand1 = new SqlCommand(sSQL, sqlConnection1);
//创建一个SqlDataAdapter对象
sqlDataAdapter1 = new SqlDataAdapter(sSQL, sqlConnection1);
// 创建一个DataSet对象
dataSet1 = new DataSet();
sqlDataAdapter1.Fill(dataSet1, "admin");
dataGridView1.DataSource = dataSet1.Tables["admin"];
sqlCommand1.CommandText = "SELECT storage FROM room order by storage";
GetCustomID();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == cbxID.Text)
dataGridView1.Rows[i].Selected = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
String constr = "Data Source=localhost; Initial Catalog=WZGL_20052070;Integrated Security=true";
SqlConnection scon = new SqlConnection(constr);
SqlDataAdapter sa = new SqlDataAdapter();
SqlCommand storescommand = new SqlCommand();
storescommand.CommandText = "select *from store";
storescommand.Connection = scon;
sa.SelectCommand = storescommand;
DataSet ds = new DataSet();
sa.Fill(ds, "admin");
dataGridView1.DataSource = ds.Tables["admin"];
MessageBox.Show("若没显示库号,可按添加按钮", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
scon.Close();
}
private void button3_Click(object sender, EventArgs e)
{
//MainFrm mainform = new MainFrm();
//mainform.Show();
//this.Hide();
this.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
cbxID.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
string a = "SELECT count(outquantity) FROM outstore WHERE storage = " + "'" + cbxID.Text + "'";
String constr = "Data Source=localhost; Initial Catalog=WZGL_20052070;Integrated Security=true";
SqlConnection scon = new SqlConnection(constr);
scon.Open();
SqlCommand sqlcmd = new SqlCommand(a, scon);
SqlDataReader sdr;
//sqlConnection1.Open();
sdr = sqlcmd.ExecuteReader();
if (sdr.Read())
{
if (sdr[0].ToString() == "0")
{
MessageBox.Show("当前还没有商品出库,请按“查看2”按钮", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("请按“查看1”按钮", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
} sdr.Close();
sqlConnection1.Close();
}
private void button5_Click(object sender, EventArgs e)
{
try
{
string f = "SELECT sum(nowquantity) FROM outstore WHERE storage = " + "'" + cbxID.Text + "'";
SqlCommand scon = new SqlCommand(f, sqlConnection1);
SqlDataReader dr;
sqlConnection1.Open();
dr = scon.ExecuteReader();
if (dr.Read())
{
int g = int.Parse(dr[0].ToString());
int y = 1000 - g;
textBox1.Text = y.ToString();
}
dr.Close();
sqlConnection1.Close();
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button6_Click(object sender, EventArgs e)
{
try
{
string f = "SELECT sum(quantity) FROM room WHERE storage = " + "'" + cbxID.Text + "'";
SqlCommand scon = new SqlCommand(f, sqlConnection1);
SqlDataReader dr;
sqlConnection1.Open();
dr = scon.ExecuteReader();
if (dr.Read())
{
int g = int.Parse(dr[0].ToString());
int y = 1000 - g;
textBox1.Text = y.ToString();
}
dr.Close();
sqlConnection1.Close();
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
String constr = "Data Source=localhost; Initial Catalog=WZGL_20052070;Integrated Security=true";
SqlConnection scon = new SqlConnection(constr);
SqlDataAdapter sa = new SqlDataAdapter();
SqlCommand storescommand = new SqlCommand();
string s = "INSERT INTO store VALUES(" + "'" + cbxID.Text + "'," + "'" + textBox1.Text + "')";
storescommand.CommandText = s;
storescommand.Connection = scon;
sa.SelectCommand = storescommand;
DataSet ds = new DataSet();
sa.Fill(ds, "admin");
dataGridView1.DataSource = ds.Tables["admin"];
MessageBox.Show("添加成功", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException ex)
{
MessageBox.Show("更新错误:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void store_Load(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -