Access数据库列中的空白

时间:2019-01-24 17:02:13

标签: vb.net access

当我保存到数据库时。它始终保存在Microsoft Access数据库中,并且前面有空格。

可能是什么问题。

我已删除该表并创建了另一个。还是一样 “保存”页面对密码进行了加密。

    Dim strPassword As String = txtpassword.Text
    Dim strAlgorigthm As String = "MD5"


    ' Members private to this particular instance of the sandwich class


    'You can also specify these algorights RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,
    'SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512
    'SHA1Managed algorithm you must be instantiated directly like this "objAlg = new SHA1Managed()"

    Dim objAlg As HashAlgorithm = Nothing
    'Dim objAlg = New SHA1Managed(strAlgorigthm)
    objAlg = HashAlgorithm.Create(strAlgorigthm)

    ' Convert string to bytes
    Dim objData As Byte() = System.Text.Encoding.[Default].GetBytes(strPassword)

    ' Generate the hash code of password
    Dim objHashBytes As Byte() = objAlg.ComputeHash(objData)
    Dim encryppass As String
    encryppass = BitConverter.ToString(objHashBytes)
    'database connection starts from here

    Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\inventory1.accdb")
    cn.Open()
    Dim cmd As New OleDbCommand
    cmd.CommandText = "SELECT * FROM tbltest1;"
    cmd.Connection = cn
    Dim updcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("INSERT INTO tbltest1 (userid, fullname, usersname, userspassword) VALUES (' " & txtuser.Text & " ',' " & txtfullname.Text & " ',' " & txtusername.Text & " ',' " & encryppass & " ')")
    Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
    updcmd.Connection = cn
    da.SelectCommand = cmd
    da.InsertCommand = updcmd
    Dim ds As DataSet = New DataSet("tbltest1")
    Try
        'cn.Open()
        da.Fill(ds, "tbltest1")
        Dim NewRow As DataRow = ds.Tables("tbltest1").NewRow
        ' Dim countr As Integer = ds.Tables("tblusers").Rows.Count
        NewRow.Item(Microsoft.VisualBasic.Trim("userid")) = txtuser.Text
        NewRow.Item(Microsoft.VisualBasic.Trim("fullname")) = txtfullname.Text
        NewRow.Item(Microsoft.VisualBasic.Trim("usersname")) = txtusername.Text
        NewRow.Item(Microsoft.VisualBasic.Trim("userspassword")) = encryppass


        ds.Tables("tbltest1").Rows.Add(NewRow)
        'Saving to database            
        da.Update(ds, "tbltest1")
        MsgBox("Information saved successfully", MsgBoxStyle.OkOnly)
        cn.Close()
    Catch Ex As Exception
        MsgBox(Ex.Message)
        Console.WriteLine(Ex.Message)
    End Try

1 个答案:

答案 0 :(得分:0)

您将在SQL命令中的每个值之前和之后插入一个空格。例如VALUES (' " & txtuser.Text & " '。请注意'"之间的空格。

也正如LarsTech所说,不要通过将值动态附加到SQL命令中来插入数据。使用参数。