【发布时间】:2014-12-22 03:45:02
【问题描述】:
我不知道我的错误在哪里.. 我已经把 connection.close() 到处。我在 microsoft visual studio 2013 C# 中使用 access 数据库。它在没有数据库的情况下运行良好,但是当我尝试向其中添加数据库时,问题就开始出现了。
private void btnLogin_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT * from Login where [Username]='" + txtUser.Text + "' and [Password]='" + txtPass.Text + "' ";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
connection.Open();
Form main = new AdminMain();
main.Show();
this.Hide();
try
{
connection.Open();
command.Connection = connection;
command.CommandText = "INSERT into LogHisto ([Username],[LogDate],[LogTime]) values ('" + txtUser.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "')";
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
connection.Close();
}
else
{
MessageBox.Show("Username/Password is incorrect");
try
{
connection.Open();
command.Connection = connection;
command.CommandText = "INSERT into LogHisto (Username,LogDate,LogTime) values ('" + txtUser.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "')";
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
connection.Close();
}
【问题讨论】:
-
“这就是问题开始出现的地方。” - 什么问题?另外:使用参数化查询。
-
我试过了,但还是不行。我只是试图让我的表单将登录名和日期保存到数据库中,但是因为每当登录成功或没有我的代码混淆时,我都会打开和关闭它与数据库之间的连接。如果我不尝试保存登录名,它工作得很好但是当我尝试将它保存到我的数据库中的一个表中时,我遇到了死胡同。