繁体   English   中英

VS 2015中的代码中出现错误

[英]A error appeared in the code in VS 2015

我似乎对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'的未处理异常。Blockquote其他信息:'aa'附近的语法不正确。

任何帮助都很棒! 先感谢您!

编辑:问题解决了!

您在SQL中缺少=符号。

另外,您应该使用SqlParameter清理数据库输入,而不是使用字符串。 如果继续实施,您将为SQL注入做好准备。

另一项优化是SqlDataAdapter自动管理SqlConnection ,因此在使用Fill()时无需调用Open()Close() Fill()

var cmd = new SqlCommand();
cmd.CommandText = "SELECT Status FROM Login1 WHERE Username = @username AND Parola= @parola";
cmd.Parameters.AddWithValue("@username", textbox1.Text);
cmd.Parameters.AddWithValue("@parola", textbox2.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new System.Data.DataTable();
sda.Fill(dt);

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

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

您可能错过了多余的空间;)

SqlDataAdapter sda = new SqlDataAdapter(“从Login1 WHERE用户名中选择状态”'+ textBox1.Text +“'AND Parola ='” + textBox2.Text +“',co​​n);

将此行更改为

SqlDataAdapter sda =新的SqlDataAdapter(“从Login1 WHERE用户名=”中选择状态“ + textBox1.Text +”'AND Parola ='“ + textBox2.Text +”',con);

您忘记了=号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM