我正在使用vb.net。我想使用VB.NET添加Microsoft访问,但它总是说失败 [英] I am using vb.net. I want to use VB.NET to add in Microsoft access but it always says failed

查看:79
本文介绍了我正在使用vb.net。我想使用VB.NET添加Microsoft访问,但它总是说失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Data.OleDb

Public Class reg

Dim data As New OleDb.OleDbConnection

Dim da As Common.DbDataAdapter

Dim ds As DataSet = New DataSet



Private Sub Button2_Click(sender as Object,e As EventArgs)处理Button2.Click

txtmain.Show()

Me.Close()

结束子



私人子button1_Click(sender As Object,e As EventArgs)处理button1.Click



data = New OleDb.OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\ Users \evely \Documents\log.mdb)

尝试

Dim query As Integer

通话数据。打开()

query =INSERT INTO data(学生ID,名字,姓氏,地址,电子邮件)VALUES('& Txtid.Text&','& Txtfname.Text&','& Txtlname.Text&', &安培; Txtadd.Text&安培;,‘’&安培; Txte.Text& ')

Dim sqlcmd作为OleDb.OleDbCommand =新的OleDb.OleDbCommand(查询,数据)

sqlcmd.ExecuteNonQuery()

MsgBox (保存记录)

Catch ex As Exception

MsgBox(失败)

结束尝试

结束子







结束班级



我尝试了什么:



i search但它只显示关于SQL

Imports System.Data.OleDb
Public Class reg
Dim data As New OleDb.OleDbConnection
Dim da As Common.DbDataAdapter
Dim ds As DataSet = New DataSet

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
txtmain.Show()
Me.Close()
End Sub

Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click

data = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\evely\Documents\log.mdb")
Try
Dim query As Integer
Call data.Open()
query = "INSERT INTO data (Student ID,First Name,Last Name, Address, Email) VALUES('" & Txtid.Text & " ', ' " & Txtfname.Text & " ', ' " & Txtlname.Text & " ', ' " & Txtadd.Text & " ', '' " & Txte.Text & " ' )"
Dim sqlcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(query, data)
sqlcmd.ExecuteNonQuery()
MsgBox("Record Save")
Catch ex As Exception
MsgBox("Failed")
End Try
End Sub



End Class

What I have tried:

i search but it only shows about SQL

推荐答案

查询变量被声明为整数但是用字符串值提供。

此外,错误消息的文本应该是非常具有描述性的;请在您的问题中包含错误消息的文本。
query variable is declared as integer but fed with a string value.
Besides, the text of the error message should be pretty descriptive; please include the text of the error message in your question.


对于初学者,请不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



其次,你需要使用他的调试器并查看异常显示的内容 - 它将包括详细信息这将解释这个问题。



但是快速浏览一下你的SQL语句就说:修复SQL注入漏洞也可以摆脱这个问题!

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Secondly, you need to use he debugger and look at exactly what the exception is showing you - it will include details which will explain the problem.

But a quick glance at your SQL statement says: fixing the SQL Injection vulnerability will get rid of the problem as well!


这篇关于我正在使用vb.net。我想使用VB.NET添加Microsoft访问,但它总是说失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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