2014-11-06 80 views
-3

我查看了论坛并尝试了所有人提出的建议,但找不到可使此解决方案正常工作的解决方案。该程序将运行,直到它必须连接到数据库。然后,在这一点上它会弹出以下错误:SQL异常未处理,:'用户'附近语法不正确

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near 'user'.

User是在我的登录页面的用户名。

这是我的代码。任何人都可以看到我错过的任何问题?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace LoginForm 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

     SqlConnection con = new SqlConnection(@"Data Source=  (LocalDB)\v11.0;AttachDbFilename=C:\Users\Username\Documents\Data.mdf;Integrated Security=True;Connect  Timeout=30"); 
     SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Login where Username'"  +textBox1.Text + "' and Password ='"+textBox2.Text+"'",con); 
     DataTable dt = new DataTable(); 
     sda.Fill(dt); 
     if(dt.Rows[0][0].ToString() == "1") 
     { 
      this.Hide(); 
      Main ss = new Main(); 
      ss.Show(); 
     } 
     else 
     { 
      MessageBox.Show("please check Username and Password and try again."); 
     } 
    } 
} 
+3

这里是你的问题:where U sername'“'=缺少等号:'where用户名='”' – 2014-11-06 14:23:52

+5

并且_please_学会使用参数而不是串联SQL - 特别是在访问用户/密码信息时。 – 2014-11-06 14:24:45

+0

我可以看到语法错误(缺少等号)。当你修复它时,尝试登录为“丹”; DELETE FROM登录; - “,然后谷歌SQL注入。 – Joe 2014-11-06 14:25:19

回答

1

更好地使用真实SqlParameter。但首先这应有助于:

SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Login where Username ='"  +textBox1.Text + "' and Password ='"+textBox2.Text+"'",con); 

但是你的程序是非常不安全的,因为你可以得到sql-injection问题。

看看本作SqlAdapter和的SqlParameter:Getting SqlDataAdapter and SqlCommand confused

0

一位像别人说这个代码是受到SQL注入

你并不需要一个数据表来获得一个价值

SqlConnection con = new SqlConnection(@"Data Source (LocalDB)\v11.0;AttachDbFilename=C:\Users\Spyer\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"); 
SqlCommand cmd = con.CreateCommand(); 
cmd.CommandText = "Select Count(*) From Login where Username = '" + textBox1.Text + "' and Password = '" + textBox2.Text + "'"; 
con.Open(); 
Int32 ccount = (Int32)cmd.ExecuteScalar(); 
con.Close(); 

这是一个合适的方法

String connString = @"Data Source (LocalDB)\v11.0;AttachDbFilename=C:\Users\Spyer\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"); 
String sql = "Select Count(*) From Login where Username = @Name and Password = @Password"; 
Int32 ccount; 
using (SqlConnection conn = new SqlConnection(connString)) 
{ 
    SqlCommand cmd = new SqlCommand(sql, conn); 
    cmd.Parameters.Add("@Name", SqlDbType.VarChar); 
    cmd.Parameters["@Name"].Value = textBox1.Text; 
    cmd.Parameters.Add("@Password", SqlDbType.VarChar); 
    cmd.Parameters["@Password"].Value = textBox2.Text; 
    try 
    { 
     conn.Open(); 
     ccount = (Int32)cmd.ExecuteScalar(); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
}