none
怎樣使字串加密後再存入資料庫 RRS feed

  • 問題

  • 在論壇上看到小朱前輩的代碼:

    Imports System.Security.Cryptographics
    Imports System.Text

    Public Function HashPassword(ByVal PasswordString) As String

        Dim hashAlgorithm As New SHA384Managed
        Dim srcPassword As Byte() = Encoding.ASCII.GetBytes(PasswordString)
        Dim destPassword As Byte() 
        Dim hashedPasswordString As String

        destPassword = hashAlgorithm.ComputeHash(srcPassword)
        hashedPassword = Encoding.ASCII.GetString(destPassword)

        hashAlgorithm = Nothing
        Return hashedPassword

    End Function

    我現在的問題是,輸入密碼的這個欄位是用DataBindings直接系結在資料欄位上的,要怎樣設定才可以讓密碼跑過這個程式後再存入資料庫.

    2006年12月21日 下午 12:33

解答

  • :::在SQL Server裡建一個Table(test6),欄位為Id,Name

    在表單上放置三個按鈕及一個TextBox,若有更新到資料時,則多做處理(加密)

        Dim cn As New SqlConnection("server=db-machine;database=sqltest;user id=sa;password=sa")
        Dim cmd As New SqlCommand("select * from test6", cn)
        Dim WithEvents da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim cm As CurrencyManager
        Dim cb As New SqlCommandBuilder

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            cb.DataAdapter = da
            da.SelectCommand = cmd
            da.Fill(ds, "test6")

            cm = Me.BindingContext(ds, "test6")
            cm.Position = 0
            TextBox1.DataBindings.Add("text", ds, "test6.name")
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            cm.Position -= 1
        End Sub

        Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            cm.Position += 1
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            '更新回資料庫
            da.Update(ds, "test6")
        End Sub

        Private Sub da_RowUpdating(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlRowUpdatingEventArgs) Handles da.RowUpdating
            '更新回資料庫時做一些處理流程
            e.Row("name") &= "---ABC"
        End Sub

    2006年12月21日 下午 02:32