我查看了论坛并尝试了所有人提出的建议,但找不到可使此解决方案正常工作的解决方案。该程序将运行,直到它必须连接到数据库。然后,在这一点上它会弹出以下错误: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.");
}
}
}
这里是你的问题:where U sername'“'=缺少等号:'where用户名='”' – 2014-11-06 14:23:52
并且_please_学会使用参数而不是串联SQL - 特别是在访问用户/密码信息时。 – 2014-11-06 14:24:45
我可以看到语法错误(缺少等号)。当你修复它时,尝试登录为“丹”; DELETE FROM登录; - “,然后谷歌SQL注入。 – Joe 2014-11-06 14:25:19