好吧,我是一名头痛的编程学生,希望有人能够治好。如何在ASP.NET中使用Access中的JOIN SQL语句?
我目前正在创建使用ASP.NET在Microsoft Visual Studio中的一个网站,一个项目,这里是我的问题:
我在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 = '[email protected]' 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();
}
}
因此,如果任何人都可以告诉我,如果我的查询语句是错误的,如何纠正它或者它的其他什么东西完全,我将非常感激。
你可以显示的结果集你有正确的输出应该是什么相处? – JeffO 2014-12-05 18:39:31
我得到的结果是空的,因为有一些例外在某处逗弄我。数据库的结果应该是0-7之间的数字,它代表存储8个安全问题的ArrayList的索引。然后用它从数组中获取该值,并将其存储在“问题”变量中,然后返回。 – 2014-12-05 19:07:23