【发布时间】:2014-09-09 20:12:04
【问题描述】:
我正在制作一个考勤系统,但我不知道如何在数据库中存储日期时间。如果有人有任何考勤代码,我真的需要一些帮助
这是我的代码:
con = newSqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
dt = new DataTable();
cmd = new SqlCommand(@"SELECT EmpID FROM data WHERE EmpID='" + Code.Text + "'", con);
con.Open();
sdr = cmd.ExecuteReader();
int count = 0;
while (sdr.Read())
{
count = count + 1;
}
con.Close();
if (count == 1)
{
con.Open();
DateTime dtn = DateTime.Now;
dtn = Convert.ToDateTime(DateTime.Now.ToString("hh:mm"));
string query = @"INSERT INTO Time (TimeIn) Values ('" + dtn + "')";
cmdd = new SqlCommand(query, con);
sdr = cmdd.ExecuteReader();
sdr.Read();
dataGridView.DataSource = databaseDataSet.Time ;
con.Close();
MessageBox.Show("Verify Ok");
}
else
{
MessageBox.Show("Please Try Again");
}
【问题讨论】:
-
使用参数化查询来防止sql注入
-
你的表Time中TimeIn列的数据类型是什么?
-
考虑使用 SQL Server 存储过程,并将您的一些逻辑从 c# 代码转移到存储过程。它将使您的 c# 代码更具可读性和轻量级,并且还可以保护您免受一些 sql 注入。