VS 2015中的代码中出现错误

时间:2017-01-09 19:18:19

标签: c# visual-studio visual-studio-2015 syntax-error

我似乎对VS 2015有疑问。

它给我带来了同样的错误,我不知道为什么。我在代码下方插入。

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        textBox2.PasswordChar = '*';
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

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

    private void LogIn_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Gigabyte\\Desktop\\apps\\WindowsFormsApplication3\\WindowsFormsApplication3\\Database1.mdf;Integrated Security=True");
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT Status FROM Login1 WHERE Username'" + textBox1.Text + "'AND Parola='" + textBox2.Text + "' ", con);
        con.Close();
        DataTable dt = new System.Data.DataTable();
        sda.Fill(dt);
        if(dt.Rows.Count==1)
        {
            Form2 ss = new Form2();
            ss.Show();
        }
    }
}
}

申请表停在 sda.Fill(dt); 的行,并显示此错误:

  

Blockquote System.Data.dll中发生未处理的“System.Data.SqlClient.SqlException”类型异常   大段引用   附加信息:'aa'附近的语法不正确。

任何帮助都很棒!提前谢谢!

编辑: 解决了问题!

3 个答案:

答案 0 :(得分:0)

您的sql中缺少=个符号。

此外,您应该使用SqlParameter来清理数据库输入,而不是连接字符串。如果继续实施,您可以自行设置SQL注入。

另一个优化是{{​​1}}会自动管理您的SqlDataAdapter,因此您在使用{{1}时不需要致电SqlConnectionOpen() }

Close()

答案 1 :(得分:-1)

我想你的字符串应该是这样的:

Status FROM Login1 WHERE Username ='" + textBox1.Text + "' AND Parola='" + textBox2.Text + "'

您可能错过了额外的空格;)

答案 2 :(得分:-1)

SqlDataAdapter sda = new SqlDataAdapter(“SELECT Status FROM Login1 WHERE Username'”+ textBox1.Text +“'AND Parola ='”+ textBox2.Text +“'”,con);

将此行更改为

SqlDataAdapter sda = new SqlDataAdapter(“SELECT Status FROM Login1 WHERE Username ='”+ textBox1.Text +“'AND Parola ='”+ textBox2.Text +“'”,con);

你忘记了=符号。

相关问题