这是我认为有效的代码,但它继续在我的FROM
子句中给出语法错误。有人能帮我理解我错过的东西吗?如果没有try\catch
,则会突出显示第int result = (int)cmd.ExecuteScalar();
行。
string constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jwhite\Documents\TrainingDB.accdb";
string cmdText = "SELECT COUNT(*) FROM USER WHERE Username=@p1 AND [Password]=@p2";
using (OleDbConnection con = new OleDbConnection(constring))
using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
{
try
{
con.Open();
cmd.Parameters.AddWithValue("@p1", textBox1.Text);
cmd.Parameters.AddWithValue("@p2", textBox2.Text);
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
groupBox1.Visible = false;
groupBox2.Visible = true;
string commandText = "SELECT RIGHTS FROM USER WHERE Username=@p1 and [Password]=@p2";
using (OleDbCommand command = new OleDbCommand(commandText, con))
{
command.Parameters.AddWithValue("@p1", textBox1.Text);
command.Parameters.AddWithValue("@p2", textBox2.Text);
string query = (string)command.ExecuteScalar();
{
if (query == "Administrator")
{
toolStripMenuItem59.Enabled = true;
administratorToolStripMenuItem1.Enabled = true;
administratorToolStripMenuItem3.Enabled = true;
administratorToolStripMenuItem4.Enabled = true;
administratorToolStripMenuItem5.Enabled = true;
administratorToolStripMenuItem2.Enabled = true;
administratorToolStripMenuItem6.Enabled = true;
toolStripMenuItem92.Enabled = true;
toolStripMenuItem108.Enabled = true;
}
}
}
}
else
MessageBox.Show("Invalid Credentials, Please Try Again");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
}
答案 0 :(得分:2)
答案 1 :(得分:0)
根据http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx OleDbCommand不支持命名参数
OLE DB .NET提供程序不支持传递的命名参数 SQL语句或由a调用的存储过程的参数 CommandType设置为Text时的OleDbCommand。在这种情况下, 必须使用问号(?)占位符。例如:
SELECT * FROM Customers WHERE CustomerID = ?
因此,OleDbParameter对象添加到OleDbParameterCollection的顺序必须直接对应于命令文本中参数的问号占位符的位置。
所以参数的顺序很重要。