简体   繁体   English

“填写:SelectCommand.Connection 属性尚未初始化。”

[英]'Fill: SelectCommand.Connection property has not been initialized.'

I am using a visual studio 2022 vb.net and mssql management studio.我正在使用 visual studio 2022 vb.net 和 mssql management studio。

login form登录表单

Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles Btnlogin.Click
    cn.Open()
    cm = New SqlClient.SqlCommand("select * from table_user where username like '" & username.Text & "' and password like '" & password.Text & "'and usertype=   '" & usertype.SelectedItem & "'", cn)
    dr = cm.ExecuteReader
    sda.Fill(dt)
    If (dt.Rows.Count > 0) Then
        MessageBox.Show("You are login as " + dt.Rows(0)(2))
        If (usertype.SelectedIndex = 0) Then
            Dim a As New dashboard
            a.Show()
            Me.Hide()
        Else
            Dim b As New Admin
            b.Show()
            Me.Hide()
        End If
    Else
        MessageBox.Show("Username or Password Incorrect. CONTACT ADMINISTRATOR!")
    End If
    cn.Close()
End Sub

Module模块

Imports System.Data.SqlClient

Module Module1
    Public cn As New SqlConnection("Data Source=DESKTOP-7POF5HE\SQLEXPRESS;Initial Catalog=dict;Integrated Security=True")
    Public cm As New SqlCommand
    Public dr As SqlDataReader
    Public sda As SqlDataAdapter = New SqlDataAdapter(cm)
    Public dt As DataTable = New DataTable()
End Module

CAN YOU HELP ME TO SOLVE THIS?你能帮我解决这个问题吗?

Seems there are several issues with this code.这段代码似乎有几个问题。

In your module, you create the command object and the data adapter object. But in the form method, you create a new command object. But that will not update the data adapter to use that new command object. Class variables are reference types.在您的模块中,您创建了命令 object 和数据适配器 object。但是在表单方法中,您创建了一个新命令 object。但这不会更新数据适配器以使用该新命令 object。Class 变量是引用类型。 They just point to an object in the memory somewhere.他们只是指向 memory 某处的 object。 Your cm variable will point to the new command object, but your sda object will internally still point to the old command object.您的cm变量将指向新命令 object,但您的sda object 在内部仍将指向旧命令 object。

Furthermore:此外:

  1. You are using both a data reader and a data adapter.您正在使用数据读取器和数据适配器。 Both have their pros and cons, but you probably don't need (or want) to use them both at the same time.两者各有利弊,但您可能不需要(或不想)同时使用它们。 I assume you want to use the data adapter.我假设您想使用数据适配器。 So you can drop the dr = cm.ExecuteReader line in the form method.因此,您可以在表单方法中删除dr = cm.ExecuteReader行。

  2. Since you will probably always want to create command objects and data adapter objects on the fly (as I would), you could remove them from the module.由于您可能总是希望即时创建命令对象和数据适配器对象(就像我一样),因此您可以将它们从模块中删除。 Just create them both as local variables in your form method.只需在表单方法中将它们都创建为局部变量即可。

  3. Try to use the Using statement for such objects.尝试对此类对象Using语句。 They need to be disposed of nicely when the form method finishes.当表单方法完成时,需要很好地处理它们。 Otherwise they might keep valuable system resources in use until the .NET garbage collector disposes them (which will probably occur when you close your application, not earlier).否则,它们可能会一直使用宝贵的系统资源,直到 .NET 垃圾收集器处理它们(这可能会在您关闭应用程序时发生,而不是更早)。

  4. Also be careful with concatenating SQL statements from variables.还要小心连接来自变量的 SQL 语句。 What would happen here if you enter this text in your username textbox?: ';delete from table_user;--如果您在username文本框中输入此文本,会发生什么情况?: ';delete from table_user;--
    Hint: Do not actually try it!提示:不要实际尝试! It will try to delete all your users from the database table table_user .它将尝试从数据库表table_user中删除所有用户。 Just try to manually reproduce the SQL string that will be built in your form method.只需尝试手动重现将在您的表单方法中构建的 SQL 字符串。 This is called an SQL injection attack .这称为SQL 注入攻击 To avoid such nasty things, it's easiest to use SQL parameters in your SQL statements and pass them separately to your command object.为了避免这种讨厌的事情,最简单的方法是在 SQL 语句中使用 SQL 参数,并将它们分别传递给命令 object。

暂无
暂无

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

相关问题 错误:填充:SelectCommand.Connection属性尚未初始化 - Error: Fill: SelectCommand.Connection property has not been initialized 我目前收到此错误 System.InvalidOperationException: 'Fill: SelectCommand.Connection property has not been initialized.' - i am currently getting this error, System.InvalidOperationException: 'Fill: SelectCommand.Connection property has not been initialized.' VB.NET 2013-填充:SelectCommand.Connection属性尚未初始化 - VB.NET 2013 - Fill: SelectCommand.Connection property has not been initialized vb.net错误->填充:SelectCommand.Connection属性尚未初始化 - vb.net error -->Fill: SelectCommand.Connection property has not been initialized 错误:填充:selectcommand.connection 属性尚未被 - Error: Fill: selectcommand.connection property has not been SqlDataAdapter#Fill: `SelectCommand.connection` 属性尚未初始化 - SqlDataAdapter#Fill: `SelectCommand.connection` property has not been initilized 填充:SelectCommand 连接属性尚未初始化 - fill: SelectCommand connection property has not been initialized 在调用'Fill之前,尚未初始化SelectCommand属性。 - The SelectCommand property has not been initialized before calling 'Fill SelectCommand 属性在调用 'Fill' 之前没有被初始化的问题 - The SelectCommand property has not been initialized before calling 'Fill' problem ExecuteNonQuery:连接属性尚未初始化。 即使连接打开 - ExecuteNonQuery: Connection property has not been initialized. even with connection open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM