2014-03-06 60 views
1

我是新来的SQL和视觉工作室等,但我已经改变了一些不允许我登录到我的应用程序。每当我按下登录按钮我得到这个错误SQL“来自'的关键字附近的语法错误'。”

不正确的语法“从”

这里就是源可以是关键字附近;

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace RockPaperApp 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["New"] != null) 
     { 
      Response.Redirect("/Game.aspx"); 
     } 
    } 

    protected void RegButton_Press(object sender, EventArgs e) 
    { 
     Response.Redirect("/Register.aspx"); 
    } 

    protected void LogButton_Press(object sender, EventArgs e) 
    { 
     string username = UsernameLogTxt.Text; 

     try 
     { 
      string conn = ConfigurationManager.ConnectionStrings["UserConS"].ToString(); 
      string CommandText = "pword from data [email protected]"; 

      using (SqlConnection connection = new SqlConnection(conn.ToString())) 
      using (SqlCommand command = new SqlCommand(CommandText, connection)) 
      { 
       command.Parameters.AddWithValue("@username", username); 
       connection.Open(); 

       using (SqlDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         string realpass = reader[0].ToString(); 

         if (realpass != PasswordLogTxt.Text) 
         { 
          Response.Write("<span style='color:red'>A Wrong Username of Password has been entered.</span>"); 
         } 
         else 
         { 
          Session["New"] = UsernameLogTxt.Text; 
          Response.Redirect("/Game.aspx"); 
         } 
        } 

        if (!reader.HasRows) 
        { 
         Response.Write("No such username exists."); 
        } 
       } 
       connection.Close(); 
      } 

     } 
     catch (SqlException ex) 
     { 
      Response.Write(ex.Message); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("Register.aspx"); 
    } 
    }  
} 
+0

尽管对于这个问题可能并不重要:当提问SQL问题时**总是**添加对应于你的DBMS的标签('postgresql','oracle' ,'sql-server',...)。 ** SQL **是一种查询语言,而不是DBMS产品。 –

回答

1

您的SQL需要为:SELECT pword FROM data WHERE [email protected]。 (,FROM,WHERE)并不重要。

+0

哦,是的......谢谢!你可以告诉我这是新的.. – user2974706

2

这不是一个有效的SQL查询:

"pword from data [email protected]" 

也许你复制/粘贴一部分,当你的意思是复制/粘贴整个事情的查询?不过,它甚至不像查询的有效部分

错误告诉你问题在于关键字from,因为这是错误发生后遇到的第一件事。对于SQL错误,请始终查看错误位置之前解析的最后一件事,最后一件事是造成错误的原因。在这种情况下,关键字pword导致它,因为这不是SQL中的有效关键字或标识符,查询解析器无法理解它。

便笺: Stack Overflow上经常出现一些SQL注入漏洞的新手和经验丰富的开发人员。这是一个难得的机会,我可以亲自推荐您主动使用参数化查询,尽管您有相对较新的体验级别。我真的很感动,请保持良好的工作!

另一个侧面说明:虽然,你以纯文本格式,这是一个非常非常糟糕的事情也存储密码。散列密码并存储散列最好。然后当用户输入密码时,哈希他们输入的内容并将其与存储的哈希值进行比较。一旦你得到这个工作,我希望你能解决这个问题:)

+0

是非常多 - 这是粘贴的笔记,而不是完整的语法。谢谢! – user2974706

+1

@ user2974706:请参阅我的更新。虽然你的代码有一个错误,它也有*非常好的事情*,我希望你继续使用:) – David

+1

我们的讲师真的钻进我们。重要性卡住了:)谢谢! – user2974706

相关问题