好的,所以我是一名头疼的编程学生,希望有人能够治愈。
我目前正在Microsoft Visual Studio中为项目创建一个使用ASP.NET的网站,这是我的问题:
我在Microsoft Access中有一个数据库,其中包含用户登录数据表(名称,姓氏,电子邮件,密码)以及包含用户安全问题编号的链接表(该编号是对具有实际数据的ArrayList的引用)用户选择的问题,然后是他们提交的实际答案。
我正在使用Web服务来处理需要访问数据库的任何内容。
我想要做的是在数据库中选择安全问题编号并返回其值,但我不确定这是什么是正确的SQL语句,或者我的代码是否允许这样做。所以,如果有人能帮助我,我将非常感激。请注意,我对编程很新。
这是我的代码:
//This retrieves the security question of the user.
[WebMethod]
public string GetUserQuestion(string email)
{
try
{
//Connect to database.
this.ConnectToDatabase();
OleDbCommand cmd = conn.CreateCommand();
//The query to return the value of the index of the question that is stored in the arraylist.
cmd.CommandText = @"SELECT SecurityInfo.QuestionNumber FROM [SecurityInfo],[UserDetails] WHERE (UserDetails.Email = '" + email + "' JOIN [UserDetails] ON SecurityInfo.ID = UserDetails.ID)";
//cast the value to a string
string userQuestion = cmd.ExecuteReader().ToString();
return userQuestion;
}
catch (Exception e)
{
return e.ToString();
}
finally
{
this.DisconnectDatabase();
}
}
PS:这些方法几乎都按照它们的名字命名,因此不必将它们包含在问题中。
这是我在服务中测试此方法时遇到的错误:
<string xmlns="http://tempuri.org/">
System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression '(UserDetails.Email = 'rudivisagiex@gmail.com' JOIN [UserDetails] ON SecurityInfo.ID = UserDetails.ID)'. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.ExecuteReader() at PTWService.GetUserQuestion(String email) in c:\Users\Rudi\Documents\Visual Studio 2010\WebSites\PTWService\App_Code\PTWService.cs:line 108
</string>
我将我的代码改为此(摆脱了错误,但我仍然没有从数组中得到我想要的东西):
//This retrieves the security question of the user.
[WebMethod]
public string GetUserQuestion(string email)
{
try
{
//Connect to database.
this.ConnectToDatabase();
OleDbCommand cmd = conn.CreateCommand();
//The query to return the value of the index of the question that is stored in the arraylist.
cmd.CommandText = @"SELECT SecurityInfo.QuestionNumber FROM [SecurityInfo] WHERE (UserDetails.Email = '" + email + "') JOIN [UserDetails] ON SecurityInfo.ID = UserDetails.ID";
//Parse the value to a int
int UserQuestionNr = Int32.Parse(cmd.ExecuteReader().ToString());
//Get the question from the array.
string Question = (string)questionArray[UserQuestionNr];
return Question;
}
catch (Exception)
{
return null;
}
finally
{
this.DisconnectDatabase();
}
}
因此,如果有人能告诉我我的查询陈述是否错误以及如何纠正它或者完全是其他事情我会非常感激。
答案 0 :(得分:2)
不确定您的期望。你收到的纪录太少了吗?
这就是我在通常的SQL代码中所期望的。
"SELECT SecurityInfo.QuestionNumber
FROM [SecurityInfo]
INNER JOIN [UserDetails]
ON SecurityInfo.ID = UserDetails.ID
WHERE (((UserDetails.Email) = '" + email + "'));"
您还应检查发送到数据库的字符串,以便进行故障排除。
答案 1 :(得分:0)
这是解决方案,如果有人想知道:
[WebMethod]
public string GetUserQuestion(string email)
{
try
{
//Connect to database.
this.ConnectToDatabase();
OleDbCommand cmd = conn.CreateCommand();
//The query to return the value of the index of the question that is stored in the arraylist.
cmd.CommandText = "SELECT SecurityInfo.QuestionNumber FROM [SecurityInfo] INNER JOIN [UserDetails] ON SecurityInfo.ID = UserDetails.ID WHERE (((UserDetails.Email) = '" + email + "'));";
OleDbDataReader reader = cmd.ExecuteReader();
//Only needs to be read once since only one row will always be returned.
reader.Read();
int questionNumber = (int)reader["QuestionNumber"];
//Get the question from the array.
string Question = (string)questionArray[questionNumber];
return Question;
}
catch (Exception)
{
return null;
}
finally
{
this.DisconnectDatabase();
}
}