| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 746 人关注过本帖
标题:在vb.net里的SQL数据无法现时更新
只看楼主 加入收藏
tan7128
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-3-16
收藏
 问题点数:0 回复次数:0 
在vb.net里的SQL数据无法现时更新
小的是新手, 还有很多东西不清楚. 请原谅我问些愚蠢的问题. 谢谢.


在clsConnectDB.vb里的"Add Project Record"代码:
Public Sub AddProjectRecord(ByVal cJobNo As String, ByVal cName As String, ByVal cDescp As String, _
                                ByVal nRevNo As Integer, ByVal cPrjmgr As String, ByVal cMD As String, _
                                ByVal cRegDate As String, ByVal cStartDate As String, ByVal cCompleteDate As String)
Dim loDataAdpr As OleDbDataAdapter
        Dim lnRowAffectResult As Integer
        isConnectDB()

        Try
            loDataAdpr = New OleDbDataAdapter()
            loDataAdpr.InsertCommand = New OleDbCommand()
            loDataAdpr.InsertCommand.Connection = oConn_ProjMgt 'oConn_ProjectMgt
            loDataAdpr. = CommandType.Text
            loDataAdpr. = " INSERT INTO ProjReg " & _
                                                                            " (jobno, name, descp, revno, prjmgr, md,regdate, startdate, completedate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"

loDataAdpr.InsertCommand.Parameters.Add("@jobno", OleDbType.VarChar).Value = cJobNo
            loDataAdpr.InsertCommand.Parameters.Add("@name", OleDbType.VarChar).Value = cName
            loDataAdpr.InsertCommand.Parameters.Add("@descp", OleDbType.VarChar).Value = cDescp
            loDataAdpr.InsertCommand.Parameters.Add("@revno", OleDbType.Integer).Value = nRevNo
            loDataAdpr.InsertCommand.Parameters.Add("@prjmgr", OleDbType.VarChar).Value = cPrjmgr
            loDataAdpr.InsertCommand.Parameters.Add("@md", OleDbType.VarChar).Value = cMD
            loDataAdpr.InsertCommand.Parameters.Add("@regdate", OleDbType.VarChar).Value = cRegDate
            loDataAdpr.InsertCommand.Parameters.Add("@startdate", OleDbType.VarChar).Value = cStartDate
            loDataAdpr.InsertCommand.Parameters.Add("@completedate", OleDbType.VarChar).Value = cCompleteDate
            lnRowAffectResult = loDataAdpr.InsertCommand.ExecuteNonQuery()

            cMsgStatus = "Added record"

            lnRowAffectResult = loDataAdpr.InsertCommand.UpdatedRowSource

        Catch OleDBExceptionErr As OleDbException
            cMsgStatus = OleDBExceptionErr.Message
        End Try

    End Sub
**************************************************************************
在clsConnectDB.vb里的"Update Project Record"代码:
Public Sub UpdateProjectRecord(ByVal nID As Integer, ByVal cName As String, ByVal cDescp As String, _
                                ByVal nRevNo As Integer, ByVal cPrjmgr As String, ByVal cMD As String, _
                                ByVal cRegDate As String, ByVal cStartDate As String, ByVal cCompleteDate As String) Dim lnRowAffectResult As Integer
        Dim loDataAdpr As OleDbDataAdapter
        isConnectDB()

        Try

            loDataAdpr = New OleDbDataAdapter()
            loDataAdpr.UpdateCommand = New OleDbCommand()
            loDataAdpr.UpdateCommand.Connection = oConn_ProjMgt
            loDataAdpr. = CommandType.Text
            loDataAdpr. = " UPDATE ProjReg SET " & _
                                                        " name = ?, descp = ?, revno = ?, prjmgr = ?, md = ?, regdate = ?, startdate = ?, completedate = ?  WHERE id = ?"

  loDataAdpr.UpdateCommand.Parameters.Add("@name", OleDbType.VarChar).Value = cName
            loDataAdpr.UpdateCommand.Parameters.Add("@descp", OleDbType.VarChar).Value = cDescp
            loDataAdpr.UpdateCommand.Parameters.Add("@revno", OleDbType.Integer).Value = nRevNo
            loDataAdpr.UpdateCommand.Parameters.Add("@prjmgr", OleDbType.VarChar).Value = cPrjmgr
            loDataAdpr.UpdateCommand.Parameters.Add("@md", OleDbType.VarChar).Value = cMD
            loDataAdpr.UpdateCommand.Parameters.Add("@regdate", OleDbType.VarChar).Value = cRegDate
            loDataAdpr.UpdateCommand.Parameters.Add("@startdate", OleDbType.VarChar).Value = cStartDate
            loDataAdpr.UpdateCommand.Parameters.Add("@completedate", OleDbType.VarChar).Value = cCompleteDate
            loDataAdpr.UpdateCommand.Parameters.Add("@id", OleDbType.Integer).Value = nID

            lnRowAffectResult = loDataAdpr.UpdateCommand.ExecuteNonQuery()

            cMsgStatus = "Updated record"
        Catch OleDBExceptionErr As OleDbException
            cMsgStatus = OleDBExceptionErr.Message
        End Try

    End Sub
**************************************************************************
在frmProjectRegistration.vb里"Print"的代码:
Private Sub cmdPrint_Click(sender As System.Object, e As System.EventArgs) Handles cmdPrint.Click
        oDSreport = New dsrptDataset()
        oGenRpt = New clsGenReportData(frmMain.objDB.oConn_ProjMgt)


        cSQLstring = " SELECT jobno, name, descp, revno, regdate, md, prjmgr, " & _
                     " startdate, completedate"
                     " FROM ProjReg where id  = " & CStr(nID)

        cRptFile = "ProjectManagement_2012.rptProjectRegForm.rdlc"

        oGenRpt.PopulateData(cSQLstring)
        oDSreport.dtProject.Load(oGenRpt.oDrReport)



        If oDSreport.dtProject.Count > 0 Then
            ViewReport()
        Else
            MsgBox("No record display", MsgBoxStyle.Information, "Message")
        End If

        oDSreport.Dispose()
        oDSreport = Nothing

    End Sub
**************************************************************************
在frmProjectRegistration.vb里"Save"的代码:

Private Sub btnSaveRegistration_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveRegistration.Click
        If nDataFlag = 1 Then

            frmMain.objDB.AddProjectRecord(txtJobNo.Text, txtName.Text, txtDescp.Text, CInt(nudRevno.Value), cboPrjmgr.Text, cboMD.Text, _
                                           dtpRegdate.Text.Trim(), dtpStartdate.Text.Trim(), dtpCompletedate.Text.Trim())
                     
        Else
            frmMain.objDB.UpdateProjectRecord(nID, txtName.Text, txtDescp.Text, CInt(nudRevno.Value), cboPrjmgr.Text, cboMD.Text, _
                                            dtpRegdate.Text.Trim(), dtpStartdate.Text.Trim(), dtpCompletedate.Text.Trim())

        End If

       MsgBox(frmMain.objDB.cMsgStatus, MsgBoxStyle.Information, "Message")      
    End Sub
**************************************************************************
在frmProjectRegistration.vb里"Load Project"的代码:

Private Sub Load_Project(ByVal nID As Integer)
        Dim lcSQL As String
        Dim lnComboboxIndex As Integer

        lcSQL = " SELECT * FROM projreg WHERE id = " & nID

        frmMain.objDB.QueryRecord(lcSQL)
        While frmMain.objDB.oQuery.Read()
            If frmMain.objDB.oQuery.HasRows And Not frmMain.objDB.oQuery.IsDBNull(0) Then

                txtJobNo.Text = frmMain.objDB.oQuery.GetString(1)
                txtName.Text = frmMain.objDB.oQuery.GetString(2)
                txtDescp.Text = frmMain.objDB.oQuery.GetString(3)
                nudRevno.Value = frmMain.objDB.oQuery.GetInt32(91)

                lnComboboxIndex = cboPrjmgr.FindStringExact(frmMain.objDB.oQuery.GetString(29))
                cboPrjmgr.SelectedIndex = lnComboboxIndex
                lnComboboxIndex = cboMD.FindStringExact(frmMain.objDB.oQuery.GetString(30))
                cboMD.SelectedIndex = lnComboboxIndex

                If Not String.IsNullOrWhiteSpace(frmMain.objDB.oQuery.GetString(31)) Then
                    dtpRegdate.Text = frmMain.objDB.oQuery.GetString(31)
                Else
                    dtpRegdate.CustomFormat = " "
                    dtpRegdate.Text = ""
                End If

                If Not String.IsNullOrWhiteSpace(frmMain.objDB.oQuery.GetString(32)) Then
                    dtpStartdate.Text = frmMain.objDB.oQuery.GetString(32)
                Else
                    dtpStartdate.CustomFormat = " "
                    dtpStartdate.Text = ""
                End If

                If Not String.IsNullOrWhiteSpace(frmMain.objDB.oQuery.GetString(33)) Then
                    dtpCompletedate.Text = frmMain.objDB.oQuery.GetString(33)
                Else
                    dtpCompletedate.CustomFormat = " "
                    dtpCompletedate.Text = ""
                End If            
            End If
        End While
    End Sub
**************************************************************************
以上的代码皆可正常操作, 唯一的问题就是它不能立即更新. 需要出去"Add New Project"再进回去才可以看得到之前已经加进去record...
请问大虾们, 我应该怎么样才能让它自动更新SQL里面的record呢?

谢谢.
2012-08-16 14:19
快速回复:在vb.net里的SQL数据无法现时更新
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018267 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved