【发布时间】:2014-12-05 17:11:03
【问题描述】:
好的,所以我是一名编程学生,头疼,希望有人能够治愈。
我目前正在为一个项目使用 Microsoft Visual Studio 中的 ASP.NET 创建一个网站,这是我的问题:
我在 Microsoft Access 中有一个数据库,其中包含用户登录数据表(姓名、姓氏、电子邮件、密码)和一个包含用户安全问题编号的链接表(该数字是对具有实际问题),然后是他们提交的实际答案。
我正在使用 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-7 之间的数字,它表示存储 8 个安全问题的 ArrayList 的索引。然后使用它从数组中获取该值并将其存储在“问题”变量中,然后返回该变量。