我有一个命令可以计算过期书籍,但我只希望它在特定的人身上。我的命令是在表 Borrowbook 中插入罚款。我还将代码放在 datagridview 将显示数据的按钮中。我的代码是这样的:
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [Student ID], ISBN, Title, Date, [Due Date], Penalty FROM Borrowbook;", con);
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable Records = new DataTable();
sda.Fill(Records);
BindingSource bsource = new BindingSource();
bsource.DataSource = Records;
dataGridView1.DataSource = bsource;
sda.Update(Records);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (dateTimePicker2.Value < DateTime.Now)
{
cmd.CommandText = "INSERT INTO Borrowbook (Penalty) VALUES (@Penalty)";
SqlParameter p1 = new SqlParameter("@Penalty", SqlDbType.Int);
p1.Value = 50;
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery();