【问题标题】:How to use a JOIN SQL statement in ASP.NET using Access?如何使用 Access 在 ASP.NET 中使用 JOIN SQL 语句?
【发布时间】: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 的索引。然后使用它从数组中获取该值并将其存储在“问题”变量中,然后返回该变量。

标签: c# asp.net sql ms-access


【解决方案1】:

不确定您的期望。您是否获得了太少/太多的记录。

这是我通常在 Access 的 sql 代码中所期望的。

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

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

【讨论】:

  • 感谢您的回答。 sql代码工作得很好!我现在正在排队。再次感谢您的麻烦。我只是在从数据库中获取返回的行并将该值放入变量中时遇到了一些问题。但我还在学习,所以我会努力学习,直到我赢了嘿嘿。但基本上你帮我解决了我的主要问题。
  • 我发现我忘了使用 DataReader 中的 Read() 方法。那和你更正的sql。我现在准备好了。 THNX
  • -1 因为这个 sql 注入是在没有警告的情况下传递的。
  • @EsbenSkovPedersen - 很好,使用参数。希望代码不会很快发布到企业中。
  • @EsbenSkovPedersen - 我只是不喜欢冒着用太多最佳实践来劝阻初学者程序员的风险。震惊 MS Access 巨魔并没有让人们知道他们的存在。
【解决方案2】:

如果有人想知道,这就是解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2017-12-20
    • 2016-04-02
    相关资源
    最近更新 更多