只是想知道在使用这种形式的数据库命令/连接时是否存在任何典型问题。有什么“更好”的吗?任何其他可能帮助我提高我的 TSQL/C# 技能的东西都将不胜感激!谢谢!
private void Approval_Status(object sender, EventArgs e)
{
Button Approval = (Button)sender;
/*
* Boolean determining if the request was approved or denied
*/
Boolean Status = false;
if (ValidateApproval(Approval.Text.Trim().ToUpper()) == true)
{
SqlCommand cmd0 = new SqlCommand();
cmd0.Connection = db.con(user.Authority);
cmd0.CommandType = CommandType.Text;
cmd0.CommandText = "UPDATE [TBL_REQUEST] " +
"SET [TBL_REQUEST].[REQUEST_STATUS]=@Status, [TBL_REQUEST].[APPROVED_BY]=@Approver, " +
"[TBL_REQUEST].[DATE_APPROVED]=@Date, [TBL_REQUEST].[PRINTED_NAME]=@Name, " +
"[TBL_REQUEST].[TITLE]=@Title, [TBL_REQUEST].[PTO_USED]=@Used " +
"WHERE [TBL_REQUEST].[ID]=@ID; ";
if (Approval.Text.ToUpper() == codes.RequestApproved)
{
cmd0.Parameters.AddWithValue("@Status", SqlDbType.VarChar).Value = codes.RequestApproved;
Status = true ;
}
else
{
cmd0.Parameters.AddWithValue("@Status", SqlDbType.VarChar).Value = codes.RequestDenied;
Status = false;
}
cmd0.Parameters.AddWithValue("@Approver", SqlDbType.VarChar).Value = user.User;
cmd0.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = DateTime.Today.ToShortDateString();
cmd0.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = txtApproval.Text.Trim();
cmd0.Parameters.AddWithValue("@Title", SqlDbType.VarChar).Value = user.Title;
cmd0.Parameters.AddWithValue("@Used", SqlDbType.Float).Value = (float)nudUsed.Value;
cmd0.Parameters.AddWithValue("@ID", SqlDbType.VarChar).Value = txtID.Text.Trim();
/*
* Execute our non-query
*/
db.conEstablished.Open();
cmd0.ExecuteNonQuery();
db.conEstablished.Close();
/*
* Dispose our resources
*/
cmd0.Dispose();
ClearRequestsPanel();
/*
* Inform our user of a successful update
*/
if (Status == true)
{
MessageBox.Show(msg.RequestApproved);
}
else if (Status == false)
{
MessageBox.Show(msg.RequestDenied);
}
}