2017-01-03 38 views
0

我尝试在vb.net中创建一个登录表单中使用SQLite与此代码vb.net登录使用SQLite

Imports System.Data.SQLite 

Public Class frmLogin 


    Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click 

     Dim table As DataTable = Nothing 
     Dim da As New SQLiteDataAdapter("Select * from user_tbl where username='" & txtuser.Text & "'and pass_id='" & txtpass.Text & "'", myconn) 
     Try 
      If txtuser.Text = "" And txtpass.Text = "" Then 
       MessageBox.Show("Please fill Username and Password", "Important", MessageBoxButtons.OK, MessageBoxIcon.Information) 
       txtuser.Focus() 

      Else 
       da.Fill(table) 
       If table.Rows.Count > 0 Then 
        frmMain.Show() 
        Me.Close() 
       Else 
        MessageBox.Show("login not successful") 
       End If 

       da.Dispose() 
      End If 

     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     End Try 
    End Sub 

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     ConnectME() 
    End Sub 
End Class 

,但我一直在第26行得到一个错误“da.fill(表)”

下面是错误说:

enter image description here

+0

构建SQL的唯一正确方法是使用SQL参数。像这样构建它可能会导致SQL注入攻击,数据类型不匹配,格式化问题以及像'O'Brien'这样的名称出现的错误。此外,密码永远不应该以纯文本形式存储。使用一个连接并在应用程序的整个生命周期中保持开放也是不明智的。 – Plutonix

回答

0

这里是你的代码的两行:

Dim table As DataTable = Nothing 

da.Fill(table) 

您没有代码介于该变量之间,它将一个对象分配给该table变量。你为什么惊讶这里有一个问题?如果我告诉你填满一个包,然后递给你没有包,那么你到底要填充它呢?你不能,因为你不能填充一个不存在的包,但你希望你的应用能够填充不存在的DataTable

+0

对不起,实际删除“=无”,但我仍然得到相同的问题 –

+0

删除这没有帮助。默认情况下,变量仍然是“Nothing”。如果你没有给该变量赋一个'DataTable',那么它就不会引用'DataTable'。我不得不想知道,如果您没有看到创建的DataTable,那么使用数据适配器有多少例子。我只是Binged“vb.net填充数据表”,第一场比赛是一个SO线程,在接受的答案的代码中包含了“DataTable”的创建和填充。我会认为这样的搜索是最基本的研究。 – jmcilhinney