问候。
因为,我是vb.net的新手,我正在尝试使用本地sql databsae创建一个简单的登录表单,并且当我将查询字符串传递给我时,对象引用未设置为对象的实例时出现错误命令文本如下所示:
Public con As SqlConnection = Nothing
Public cmd, cmd1 As SqlCommand
Public sqlrdr As SqlDataReader = Nothing
Public sqladp As SqlDataAdapter
Public ds As DataSet
Public dtbl As DataTable
Public query As String
我在模块中定义了这些,下面的代码是登录形式
Dim frmHomesMarine As New HomesMarine
Dim connectionString As String = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Dim txtUserId = Trim(UsernameTextBox.Text)
Dim txtUserPwd = Trim(PasswordTextBox.Text)
Try
MessageBox.Show("Hello", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
con = New SqlConnection(connectionString)
con.Open()
query = "SELECT UserId, UserPassword FROM TBLUsers WHERE UserId ='" & txtUserId & "';"
cmd.CommandText = query
cmd.Connection = con
sqladp.SelectCommand = cmd
sqlrdr = cmd.ExecuteReader()
While sqlrdr.Read
If sqlrdr.HasRows Then
MessageBox.Show("Table has some rows", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
我从app.config文件中获取了连接字符串
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MyConn"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbHomesMarine.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
我用用户友好名称“MyConn”更改了名称 任何人都知道错误的原因