文本框中的数据未保存在SQL数据库中

时间:2015-07-22 17:29:13

标签: sql-server vb.net

我不确定这是我还是我的代码有问题。每当我尝试添加一个新帐户时,它都会被“保存”并显示在我的datagridview中,但是,当我检查数据集或表数据时,它没有被保存(或更新)。我正在使用Visual Studio 2013,我刚刚查看了我的Microsoft SQL版本 - 2012。我做了我在这里和其他网站上找到的内容,例如取消选中“防止保存需要重新创建表的更改”;而且我也改变了“复制到输出目录以复制如果更新”,哦,我也改变了| DataDirectory |到2 mdf文件的路径。

很抱歉,如果我问的是之前提出过的问题。而且......这是我的代码(这个代码用于更改密码形式):

    If txtNewPass.Text = txtRetyped.Text Then
            Dim strSql As String

            Try
                Dim objAdap As SqlDataAdapter
                Dim objDt As New DataTable

                strConn = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FHGES.mdf;Integrated Security=True"
                objConn = New SqlConnection(strConn)

                Dim s As String = "SELECT * FROM tblAccounts WHERE UName COLLATE Latin1_General_CS_AS='" & frmLogin.txtUname.Text & "'AND PWord COLLATE Latin1_General_CS_AS='" & frmLogin.txtPassword.Text & "';"
                objComm = New SqlCommand(s, objConn)
                objAdap = New SqlDataAdapter(objComm)
                objAdap.Fill(objDt)

                lblUName.Text = objDt.Rows(0)(4).ToString

                strSql = "UPDATE tblAccounts SET PWord='" & txtRetyped.Text & "' WHERE UName='" & lblUName.Text & "'"

                objComm = New SqlCommand(strSql, objConn)
                objConn.Open()
                objComm.ExecuteNonQuery()
                objConn.Close()

                MessageBox.Show("Saved new password, please re-login", "FHGES", MessageBoxButtons.OK)

                frmLogin.Show()
                frmLogin.txtPassword.Clear()
                frmLogin.txtPassword.Focus()
                Me.Hide()

            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                If objConn.State = ConnectionState.Open Then
                    objConn.Close()
                End If
            End Try

        Else
            MessageBox.Show("Password don't match!", "FHGES", MessageBoxButtons.OK)
            txtNewPass.Clear()
            txtRetyped.Clear()
            txtNewPass.Focus()
        End If

0 个答案:

没有答案