这是一个库存盘点系统。系统是使用C#创建的.net和SQL Server 2014正在使用中。
我有“宝石”的股票条目
ID | Stock_No | No_of_Pieces | Description | Weight | Cost |
然后我的用户管理器只能由管理员
控制ID | User_Type | User_Name |Password | Create_Date |
两个条目都有一个网格视图
管理员如何监控哪个用户输入股票请给我一个建议如何做到这一点。
这是我的登录页面代码:
try
{
SqlCommand selectCommand = new SqlCommand(" Select * from New_User where User_Name=@USER_ID and Password=@PASS", conn);
selectCommand.Parameters.Add(new SqlParameter("USER_ID", txtusername.Text.ToString()));
selectCommand.Parameters.Add(new SqlParameter("PASS", txtpassword.Text.ToString()));
string UserType = null;
SqlDataReader reader = selectCommand.ExecuteReader();
bool rowfound = reader.HasRows;
if (rowfound)
{
while (reader.Read())
{
UserType = reader["User_Type"].ToString().Trim();
if (UserType == "Administrator")
{
MessageBox.Show("Welcome ", "Admin Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
Admin_Menu frm = new Admin_Menu();
frm.Show();
this.Hide();
}
else if (UserType == "StockController")
{
MessageBox.Show("Welcome ", "User Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
Stocks_Gems frm = new Stocks_Gems();
frm.Show();
this.Hide();
}
}
}
else
{
MessageBox.Show(" Invalid User Or Password ", "Login ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("error login " + ex);
}
这是我的股票入口宝石代码:
private void btnsave_Click(object sender, EventArgs e)
{
try
{
conn.Close();
conn.Open();
string commandText = "INSERT INTO Stock_Gems VALUES(@Stock_Type,@stock_no,@No_of_pieces,@Gem_Type,@Weight,@image,@Cost,@Created_Date,@Updated_Date)";
SqlCommand command = new SqlCommand(commandText, conn);
command.Parameters.Add("@Stock_Type", SqlDbType.VarChar);
command.Parameters["@Stock_Type"].Value = Stock_Type.Text;
command.Parameters.Add("@stock_no", SqlDbType.VarChar);
command.Parameters["@stock_no"].Value = txtstock_no.Text;
command.Parameters.Add("@No_of_pieces", SqlDbType.Int);
command.Parameters["@No_of_pieces"].Value = txtno_of_peices.Text;
command.Parameters.Add("@Gem_Type", SqlDbType.NVarChar);
command.Parameters["@Gem_Type"].Value = txt_gems.Text;
command.Parameters.Add("@Weight", SqlDbType.Float);
command.Parameters["@Weight"].Value = txt_weight.Text;
MemoryStream stream = new MemoryStream();
pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
command.Parameters.AddWithValue("@image", pic);
command.Parameters.Add("@Cost", SqlDbType.Decimal);
command.Parameters["@Cost"].Value = txt_cost.Text;
command.Parameters.Add("@Created_Date", SqlDbType.DateTime);
command.Parameters["@Created_Date"].Value = label11.Text;
command.Parameters.Add("@Updated_Date", SqlDbType.DateTime);
command.Parameters["@Updated_Date"].Value = label11.Text;
command.ExecuteNonQuery();
conn.Close();
if (cmbStockType.SelectedIndex == 0)
_lastUG++;
else
_lastMG++;
saveLastNumbers();
MessageBox.Show("You've inserted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
用户经理代码:
try
{
conn.Open();
String InsertQuery = "INSERT INTO New_User VALUES('" + combo_usertype.Text + "','" + txtusername.Text
+ "', '" + txtpassword.Text + "', '" + label11.Text + "')";
SqlDataAdapter execute = new SqlDataAdapter(InsertQuery, conn);
execute.SelectCommand.ExecuteNonQuery();
MessageBox.Show("You've inserted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
SqlDataAdapter data = new SqlDataAdapter("Select * from New_User", conn);
DataTable dt = new DataTable();
data.Fill(dt);
dataGridView1.DataSource = dt;
combo_usertype.Text = "";
txtusername.Text = "";
txtpassword.Text = "";
txtcon_password.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
答案 0 :(得分:0)
请正确澄清您的问题。据我所知,您希望跟踪用户输入信息。您可以使用全局变量在登录后存储 userID ,直到用户关闭应用
您可以添加包含全局变量的类:
public static class GlobalValue
{
public static int userID= 0;
}
成功登录后,您可以将用户ID 存储在 GlobalValue 中: GlobalValue.userID ="登录用户的用户ID"
并插入数据库行或使用 userID 的 GlobalValue.UserID 强>
您需要在 stock 表中添加一列来记录用户ID。