简体   繁体   English

C# 中的 System.Data.dll 中发生了“System.InvalidOperationException”类型的未处理异常

[英]An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll in C#

Code :代码 :

private void button2_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    SqlConnection CON = new SqlConnection(@"Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\stud\Documents\ronak.mdf;Integrated Security=True;Connect Timeout=30");

      
    SqlCommand cmd = new SqlCommand("Select * from Table where username= ' " + textBox1.Text + "' and password= ' " + textBox2.Text + "' ", CON);
         
    SqlDataReader sda = cmd.ExecuteReader();

    dt.Load(sda);
    if (dt.Rows[0][0].ToString() == "1")
     {

        this.Hide();
        login2 rk = new login2();
        rk.Show();
     }
     else
     {
        MessageBox.Show("please chack you username and password");
     }
 }

This code is totally true but I have face some problem.这段代码是完全正确的,但我遇到了一些问题。

Your code has quite a number of different issues:您的代码有很多不同的问题:

  • Your connection string is missing = after Data Source您的连接字符串丢失=Data Source之后
  • You need to actually open the connection.您需要实际打开连接。
  • Do not use AttachDbFilename instead create and connect to a normal database.不要使用AttachDbFilename来创建并连接到普通数据库。
  • Do not store plain-text passwords.不要存储纯文本密码。 Salt and hash them instead.盐和哈希代替。 Then compare the hash server-side, do not return it to the client.然后比较服务器端的hash,不要返回给客户端。
  • You don't need a DataTable or DataAdapter , you can just use ExecuteScalar to retrieve a single value.您不需要DataTableDataAdapter ,您只需使用ExecuteScalar来检索单个值。
  • Dispose the connection and command with using . using处理连接和命令。
  • Do not inject data into your queries.不要将数据注入到您的查询中。 Use parameters instead.改用参数。
const string query = @"
Select 1
from Table
where username= @username
  and password= @password
";

using (var CON = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=ronak;Integrated Security=True;Connect Timeout=30"))
using (var cmd = new SqlCommand(query, CON))
{
    cmd.Parameters.Add("@username", SqlDbType.NVarChar, 100).Value = textBox1.Text;
    cmd.Parameters.Add("@password", SqlDbType.VarBinary, 256).Value = SaltAndHashPassword(textBox2.Text, textBox1.Text);
    CON.Open();
    var exists = cmd.ExecuteScalar() == 1;
    CON.Close();

    if (exists)
    {
        this.Hide();
        login2 rk = new login2();
        rk.Show();
    }
    else
    {
        MessageBox.Show("please chack you username and password");
    }
}

First of all you forget = after Data source in sqlConnection Second you are using bad names for variables Third you dont need to use SqlCommand, you can replace it with SqlDataAdapter it s more simple Fourth you must use ( Using ) to dispose connection首先你忘记=在sqlConnection中的数据源之后第二你为变量使用了错误的名称第三你need to use SqlCommand, you can replace it with SqlDataAdapter it更简单第四你必须使用(Using)来处理连接

Here`s the full code这是完整的代码

  private void button2_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\stud\Documents\ronak.mdf;Integrated Security=True;Connection  Timeout=30"))
            {
                connection.Open();
                using (SqlDataAdapter adapter = new SqlDataAdapter($"Select * from Table where username= '{textBox1.Text}' and password= '{textBox2.Text}' ", connection))
                {
                    adapter.Fill(table);

                    if (table.Rows.Count == 0)
                    {
                        MessageBox.Show("please chack you username and password");
                        return;
                    }
                    if (table.Rows[0][0].ToString() == "1")
                    {
                        this.Hide();
                        login2 rk = new login2();
                        rk.Show();
                    }
                }
            }

        }

暂无
暂无

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

相关问题 发生未处理的异常C#的system.data.dll中发生了类型'system.invalidoperationexception'的未处理异常 - An unhandled exception occured an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll in c# System.Data.dll中发生类型为'System.InvalidOperationException'的未处理异常-C#Visual Studio - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll - C# visual studio System.Data.dll 430中发生类型为'System.InvalidOperationException'的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll 430 System.Data.dll 中发生类型为“System.InvalidOperationException”的未处理异常? - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll? System.Data.dll (SqlTransaction) 中出现“System.InvalidOperationException”类型的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll (SqlTransaction) 当我尝试将数据插入文本框时,System.Data.dll中发生了类型为'System.InvalidOperationException'的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll when I tried to insert data into textbox 从文本框写入sql server 2012时,System.Data.dll中发生类型为'System.InvalidOperationException'的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll While writing from textbox to sql server 2012 将数据插入Gridview的System.Data.dll中发生类型为'System.InvalidOperationException'的异常 - An exception of type 'System.InvalidOperationException' occurred in System.Data.dll that inserting data into Gridview System.Data.dll 中发生类型为“System.InvalidOperationException”的第一次机会异常 - A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll ExecuteReader- System.Data.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理 - ExecuteReader- An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM