无法更新和删除到Access数据库中 [英] Can't Update and Delete into Access DataBase

查看:82
本文介绍了无法更新和删除到Access数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Visual Studio 2010使用ASP.Net创建Web应用程序.
我使用的数据库是Access,连接方法为OleDb

使用查询更新"或删除"时,程序不会显示任何错误,但是Access中的数据不会更改.
查询选择"和插入"时,将显示并成功插入数据.

以下代码是我使用的示例查询"Delete"..

I’m trying to create Web Application using ASP.Net via Visual Studio 2010.
The Database that I use is Access, with connection Method OleDb

When using the query "Update" or "Delete", the programs shows no errors, but the data in Access does not change.
While for query "Select" and "Insert", the data is displayed and inserted successfully.

The Following code is the sample query "Delete" that I used..

Imports System.Data.OleDb

Partial Class frmTEst
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       
    Dim dbConnection As OleDbConnection
        Dim dbCommand As OleDbCommand
        Dim sqlString As String

        dbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; _
Data Source=" & Server.MapPath("App_Data/dbKAE.mdb"))
        dbConnection.Open()

        sqlString = "DELETE FROM tblPengawasan WHERE no='31'"

        dbCommand = New OleDbCommand(sqlString, dbConnection)
        dbCommand.ExecuteNonQuery()

    End Sub
End Class



以前,我尝试使用AccessDataSource进行直接查询,但结果完全相同,无法完成更新"和删除".但是选择"和插入"可以成功执行.

我的Access配置是否有任何问题,或者问题出在编码本身..?

谢谢..



Previously I tried a direct query using AccessDataSource, but the result just the same, "Update" and "Delete" can’t be done. But the "Select" and "Insert" can be executed successfully.

Is there any problem with my Access configuration or whether the problem is in the coding itself..?

Thanks..

推荐答案

当您使用where子句(更新/删除)时,您的语句看起来不起作用,并且这种情况的可能原因可能是数据类型不匹配.

您的否"数据库中的列可能不是文本类型,而是数字,并且当您使用WHERE no =''31''时,查询不会执行(where子句中的数据类型不匹配)并且不会被注意,因为您没有处理异常.

检查您的数据库,并且列类型是否为数字/自动编号,请尝试使用
WHERE no = 31(不带单引号),它应该可以工作
Looks like your statement does not work when you user a where clause (update/Delete) and the possible reason for this could be the data type mismatch.

Your "no" column in the database may be not a text type but a number and when you use WHERE no=''31'' the query does not get executed (data type mismatch in your where clause) and goes unnoticed because you have not hanled the exceptions.

Check you database and if the column type is number / autonumber try using
WHERE no=31 (without single quote) and it should work


如果在执行代码时没有任何异常,则最有可能的错误是数据库不包含任何异常否"等于"31"的列.检查它,并确保在检查和运行代码时都引用正确的数据库.

还值得添加一些日志记录以确保您正在执行代码.我还将在整个代码中放置一个try-catch块,以确保您没有任何异常,如果有,请报告它们.
If you do not get any exception when you execute the code, then the most likely error is that the database does not contain any columns where "no" is equal to "31". Check it, and make sure you are referencing the right database both when you check, and when you run the code.

It is also worth adding some logging to make sure you are executing the code. I would also put a try-catch block around the whole code to make sure that you don''t get any exceptions, and to report them if you do.


删除
sqlString =从tblPengawasan中删除 列名 =''" + 31 (根据所需的字符串) +''"

更新
SqlCommand sqlCmd = new SqlCommand(string.Format("Update [tblPengawasan] set Column1 =``{0}''其中 列名 =''{1}'',Column1.String1,columnname.String2),dbConnection);

有关更多信息,请参阅Google ^ _ ^

快乐编码
Delete
sqlString = "DELETE FROM tblPengawasan WHERE columnname= ''" + 31(based on your desired string) + "''"

Update
SqlCommand sqlCmd = new SqlCommand(string.Format("Update [tblPengawasan ] set Column1= ''{0}'' where columnname=''{1}''", Column1.String1, columnname.String2), dbConnection );

For more information please refer to Google ^_^

Happy Coding


这篇关于无法更新和删除到Access数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆