ms访问和vs2010连接 [英] ms access and vs2010 connect

查看:54
本文介绍了ms访问和vs2010连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码。我有3个文本框。如果我在名为oznaka的第一个文本框中输入一个不是数字的值,程序将会中断。其他两个文本框(ren和rmn)都可以。如果我在第一个文本字段中输入数值,程序就不会崩溃。

为什么???

提前谢谢



Here is a code. I have a 3 textboxes. If I enter a value which is not a number into first textbox named "oznaka" the program will break. Other two textboxes (ren and rmn) are ok. And program will not crash down if I enter a numeric value into a first text field.
Why???
Thanks in advance

Public Class Form1
Dim conn As New OleDb.OleDbConnection
Private Sub RefreshData()
If Not conn.State = ConnectionState.Open Then
'open connection
conn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT Oznaka as [Oznaka], " & _
"ReN as [ReN], RmN " & _
" FROM Tabela ORDER BY Oznaka", conn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.DataGridView1.DataSource = dt
'close connection
conn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim cmd As New OleDb.OleDbCommand
If Not conn.State = ConnectionState.Open Then
'open connection if it is not yet open
conn.Open()
End If
cmd.Connection = conn
'check whether add new or update
If Me.txtOznaka.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = "INSERT INTO Tabela(Oznaka, ReN, RmN) " & _
" VALUES(" & Me.txtOznaka.Text & ",'" & Me.txtReN.Text & "','" & _
Me.txtRmN.Text & "'')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "UPDATE Tabela " & _
" SET Oznaka=" & Me.txtOznaka.Text & _
", ReN='" & Me.txtReN.Text & "'" & _
", RmN='" & Me.txtRmN.Text & "'" & _
" WHERE Oznaka=" & Me.txtOznaka.Tag
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshData()
'clear form
'Me.btnClear.PerformClick()
'close connection
conn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase
.Load
conn = New OleDb.OleDbConnection
conn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.
StartupPath & "\baza.mdb"
'
'get data into list
Me.RefreshData()
End Sub
End Class

推荐答案

插入和更新查询中存在语法错误。



您的插入和更新语句应更改如下,



插入声明 -

There''s a syntax error in insert and update query.

your insert & update statements should be changed as below,

Insert Statement -
"INSERT INTO Tabela(Oznaka, ReN, RmN) " & _
" VALUES('" & Me.txtOznaka.Text & "' ,'" & Me.txtReN.Text & "','" & _
Me.txtRmN.Text & "'')"





更新声明 -



Update Statement -

'update data in table
cmd.CommandText = "UPDATE Tabela " & _
" SET Oznaka='" & Me.txtOznaka.Text & "'" & _
", ReN='" & Me.txtReN.Text & "'" & _
", RmN='" & Me.txtRmN.Text & "'" & _
" WHERE Oznaka=" & Me.txtOznaka.Tag





尝试使用此代码..

希望这会为...工作..



Try out with this code..
Hope this will work for u..


这篇关于ms访问和vs2010连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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