用ASP.net加ACCESS的网页,都用ADO.net操作的,存在一个问题:文本框修改后存入数据库无法Update。网页的具体步骤:
Page_Load中,连接数据库,建立了一个Reader,读出表中的字段,其中WHERE用的是等于Request地址栏的id参数。读出以后给文本框绑定值,然后关闭了Reader,关闭连接。
然后用户会开始修改文本框的内容,改好后按btn_Submit。
btn_Submit_Click中,将文本框内容存入变量,连接数据库,用Command弄一个Update,Update除了id的每个字段,最后用Excute执行,没有报错,就是无法更改内容。
在另一张网页,对同一张数据表进行Insert操作就能,是不是证明数据库是可写的。具体代码如下
Imports System.Data.OleDb
Imports System.IOPartial Class News_ManageDetail
    Inherits System.Web.UI.Page    Public ori_title As String
    Public ori_article As String    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Submit.Click
        '定义变量
        Dim ar_title As String = txt_title.Text
        Dim ar_time As String = lb_NewsTime.Text
        Dim ar_auid As String = Session("UserID")
        Dim ar_attpath As String = ""
        Dim ar_attname As String = ""
        Dim ar_auname As String = lb_AuthorName.Text
        Dim ar_article As String = txt_Article.Text
        '判断文件以及操作文件
        If txt_attname.Text <> "" And FileUpload1.HasFile = False Then
            '如果没有上传文件则警告
            lb_alert.Text = "您没有上传文件。"
            lb_alert.Visible = True
            Return
        End If
        If FileUpload1.HasFile Then
            '如果没有输入文件名则警告
            If txt_attname.Text = "" Then
                lb_alert.Text = "您需要输入上传后的重命名名称。"
                lb_alert.Visible = True
                Return
            End If
            '如果超过大小则警告
            If FileUpload1.PostedFile.ContentLength > 4 * 1024 * 1024 Then
                lb_alert.Text = "您上传的文件大小过大,最大只能为4M。您上传的文件大小为 " & FileUpload1.PostedFile.ContentLength / 1024 / 1024 & " M。"
                lb_alert.Visible = True
                Return
            End If
            '如果类型不对则警告
            Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName.ToLower)
            If extension <> ".txt" And extension <> ".doc" And extension <> ".xls" And extension <> ".docx" And extension <> ".xlsx" And extension <> ".ppt" And extension <> ".pptx" And extension <> ".pdf" And extension <> ".pdf" Then
                lb_alert.Text = "您的文件类型不对,文件类型只能为: txt doc xls docx xlsx ppt pptx pdf rar 。"
                lb_alert.Visible = True
                Return
            End If
            '如果没问题,很好~
            ar_attpath = txt_attname.Text & extension
            ar_attname = txt_attname.Text
        End If
        '上传文件
        If txt_attname.Text <> "" And FileUpload1.HasFile <> False Then
            Try
                Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~\App_Data\StudentCredits.mdb") & ";Persist Security Info=True;Jet OLEDB:Database Password=unncspdpo123")
                    connection.Open()
                    Dim command_findfile As OleDbCommand = New OleDbCommand("SELECT AttPath FROM tb_News WHERE (NewsID = @Newsid)", connection)
                    command_findfile.Parameters.AddWithValue("@Newsid", Request.QueryString("newsid"))
                    Dim reader As OleDbDataReader = command_findfile.ExecuteReader()
                    reader.Read()
                    Dim ori_attpath As String = reader.GetValue(reader.GetOrdinal("AttPath")).ToString
                    reader.Close()
                    connection.Close()
                    '删除原文件
                    File.Delete(MapPath("~/UploadFiles/" & ori_attpath))
                End Using
                '添加新文件
                FileUpload1.SaveAs(MapPath("~/UploadFiles/" & ar_attpath))
            Catch ex As Exception
                lb_alert.Text = "上传失败,可能是文件名已存在,或者磁盘空间不足,请尝试更改文件名。"
                Return
            End Try
        End If
        '更新数据
        Using connection2 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~\App_Data\StudentCredits.mdb") & ";Persist Security Info=True;Jet OLEDB:Database Password=unncspdpo123")
            '更新数据库
            connection2.Open()
            Dim command_update As OleDbCommand = New OleDbCommand _
            ("UPDATE  tb_News SET tb_News.Title=@Title, tb_News.AuthorID=@AuthorID, tb_News.NewsTime=@NewsTime, tb_News.AttPath=@AttPath, tb_News.AttName=@AttName, tb_News.Article =@Article WHERE tb_News.NewsID =@NewsID", connection2)
            command_update.Parameters.AddWithValue("@Newsid", Request.QueryString("newsid"))
            command_update.Parameters.AddWithValue("@Title", ar_title)
            command_update.Parameters.AddWithValue("@AuthorID", ar_auid)
            command_update.Parameters.AddWithValue("@NewsTime", ar_time)
            command_update.Parameters.AddWithValue("@Article", ar_article)
            If txt_attname.Text <> "" And FileUpload1.HasFile <> False Then
                command_update.Parameters.AddWithValue("@AttPath", ar_attpath)
                command_update.Parameters.AddWithValue("@AttName", ar_attname)
            Else
                command_update.Parameters.AddWithValue("@AttPath", "")
                command_update.Parameters.AddWithValue("@AttName", "")
            End If
            command_update.ExecuteNonQuery()
            connection2.Close()
        End Using
        '反馈信息
        'Response.Write(<script language='javascript'>alert('修改成功!');location='News_ManageIndex.aspx';</script>)
        Response.Write(Request.QueryString("newsid") & "<br />")
        Response.Write(ar_title & "<br />")
    End Sub    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '判断是否符合权限
        If Session("Privilege") <> "admin" And Session("Privilege") <> "spdpof" Then
            Response.Write(<script language='javascript'>alert('您的权限不允许访问此界面!');location='Default.aspx';</script>)
        End If
        '设置警告字符
        lb_alert.Text = "仅在需要更新附件时再向文本框内输入文件名。"
        lb_alert.Visible = True
        '设置默认按钮
        Me.Form.DefaultButton = "cp_main$btn_Submit"
        '填入数据库内容
        Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~\App_Data\StudentCredits.mdb") & ";Persist Security Info=True;Jet OLEDB:Database Password=unncspdpo123")
            connection.Open()
            Dim command_find As OleDbCommand = New OleDbCommand("SELECT Title, Article FROM tb_News WHERE (NewsID = @Newsid)", connection)
            command_find.Parameters.AddWithValue("@Newsid", Request.QueryString("newsid"))
            Dim reader As OleDbDataReader = command_find.ExecuteReader()
            '在页面写入数据库内容
            reader.Read()
            ori_title = reader.GetValue(reader.GetOrdinal("Title")).ToString
            ori_article = reader.GetValue(reader.GetOrdinal("Article")).ToString
            reader.Close()
            '关闭数据库
            connection.Close()
        End Using
        txt_title.Text = ori_title
        lb_NewsTime.Text = DateTime.Now.ToString()
        lb_AuthorName.Text = Session("ChnName")
        txt_attname.Text = ""
        txt_Article.Text = ori_article
    End SubEnd Class