2014-12-05 74 views
0

好吧,我是一名头痛的编程学生,希望有人能够治好。如何在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(); 
    } 
} 

因此,如果任何人都可以告诉我,如果我的查询语句是错误的,如何纠正它或者它的其他什么东西完全,我将非常感激。

+0

你可以显示的结果集你有正确的输出应该是什么相处? – JeffO 2014-12-05 18:39:31

+0

我得到的结果是空的,因为有一些例外在某处逗弄我。数据库的结果应该是0-7之间的数字,它代表存储8个安全问题的ArrayList的索引。然后用它从数组中获取该值,并将其存储在“问题”变量中,然后返回。 – 2014-12-05 19:07:23

回答

2

不知道你在期待什么。你得到的记录太少/很多。

这是我通常期望在Access的SQL代码。

"SELECT SecurityInfo.QuestionNumber 
FROM [SecurityInfo] 
INNER JOIN [UserDetails] 
ON SecurityInfo.ID = UserDetails.ID 
WHERE (((UserDetails.Email) = '" + email + "'));" 

您还应该检查发送到数据库的字符串,以便进行故障排除。

+0

感谢您的回答。 sql代码很好用!我现在正在排队。为了麻烦再次感谢。我只是遇到了一些问题,从数据库中获取返回的行并将该值放入一个变量中。但是我仍然在学习,所以我会榨取它,直到我赢得嘿嘿。但基本上你帮助我解决了我的主要问题。 – 2014-12-05 19:10:13

+0

我发现我忘了使用DataReader中的Read()方法。这和你纠正的SQL。我现在定了。 THNX – 2014-12-05 19:30:04

+0

-1,因为这个sql注入被传递的时候没有任何警告。 – 2014-12-05 19:47:02

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(); 
    } 
}