如何处理以下代码中未处理的Nullreferenceexception [英] How Do I Handle Nullreferenceexception Unhandled In The Following Code
本文介绍了如何处理以下代码中未处理的Nullreferenceexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Data
公开 类表单
私有 Sub Button1_Click( ByVal sender As System 。对象, ByVal e 作为系统。 EventArgs)句柄 Button1.Click
Dim conn 正如 SqlConnection
Dim cmd 作为 SqlCommand
Dim rd As SqlDataReader
conn.ConnectionString = Data Source = localhost; Initial Catalog = user; Integrated security = true
cmd.Connection = conn
conn.Open()
cmd.CommandText = 从用户中选择登录名,密码login ='& TextBox1.Text& 'and password ='& TextBox2.Text& '
rd = cmd.ExecuteReader
如果 rd.HasRows 那么
Welcome.Show()
否则
MsgBox( 无效的登录名或密码)
结束 如果
结束 Sub
结束 类
解决方案
您声明conn
和cmd
作为变量但您没有使用SqlConnection / SqlCommand实例初始化它们。您需要使用关键字New
创建实例,或使用 ProviderFactory [ ^ ] -class获取它们的实例。
后者(使用ProviderFactory)因为那时候,如果你也使用抽象的Db ****** - 基类(如DbConnection,DbCommand,..)或接口(IDbConnection,IDbCommand,..)而不是具体和SQL-依赖于服务器的类(SqlConnection,SqlCommand,..),您将使您的代码独立于具体的数据库系统,并且能够轻松地或至少更轻松地从SQL Server更改为其他数据库系统。
此外,您不应该使用字符串连接来构建SQL语句。您的代码容易受到SQL注入的影响,而且可读性较差。请改用参数: SqlCommand.Parameters Property (System.Data.SqlClient) [ ^ ]
Dim conn As SqlConnection
语句,conn
仍为空(对于cmd
和rd $ c $相同C>)。你需要通过调用它们的构造函数给
conn
和cmd
一个值。
Dim conn As New SqlConnection()
Dim cmd As 新 SqlCommand()
注意:您不必为rd
执行此操作,因为SqlDataReader
没有构造函数,无论如何你从ExecuteReader
获得了读者。
你必须开始初始化你的对象......
Dim conn As SqlConnection
此行为SqlConnection类型的对象创建占位符,但没有创建真实对象,因此占位符是空的 - null !!!
Dim conn 作为 新 SqlConnection
有关详细信息: SqlConnection构造函数(字符串)(系统。 Data.SqlClient) [ ^
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Data
Public Class Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim rd As SqlDataReader
conn.ConnectionString = "Data Source=localhost;Initial Catalog=user;Integrated security=true"
cmd.Connection = conn
conn.Open()
cmd.CommandText = "Select login,password from user where login='" & TextBox1.Text & "'and password='" & TextBox2.Text & "'"
rd = cmd.ExecuteReader
If rd.HasRows Then
Welcome.Show()
Else
MsgBox("Invalid Login or Password")
End If
End Sub
End Class
解决方案
You declaredconn
andcmd
as variables but you didn't initialize them with instances of SqlConnection / SqlCommand. You need to either create an instance with the keywordNew
or use the ProviderFactory[^]-class to get an instance of them.
The latter (using the ProviderFactory) is recommended because then, if you also use either the abstract Db******-base-classes (like DbConnection, DbCommand, ..) or the interfaces (IDbConnection, IDbCommand, ..) instead of the concrete and SQL-Server-dependent classes (SqlConnection, SqlCommand, ..) you'll make your code independent of a concrete database system and will be able to change from SQL Server to some other DB system easily or at least more easily.
Also, you shouldn't use string concatenation for building SQL-statements. Your code becomes susceptible to SQL-injection and less readable. Use Parameters instead: SqlCommand.Parameters Property (System.Data.SqlClient)[^]
After theDim conn As SqlConnection
statement,conn
is still null (same forcmd
andrd
). You need to giveconn
andcmd
a value by calling their constructor.
Dim conn As New SqlConnection() Dim cmd As New SqlCommand()
Note: you do not have to do this forrd
, becauseSqlDataReader
has no constructor and you get the reader fromExecuteReader
anyway.
You have to start initializing your objects...
Dim conn As SqlConnection
This line creates a 'placeholder' for an object of type SqlConnection, but no real object created so the 'placeholder' is empty - null!!!
Dim conn As New SqlConnection
For more info: SqlConnection Constructor (String) (System.Data.SqlClient)[^]
这篇关于如何处理以下代码中未处理的Nullreferenceexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文