使用VB.NET 2010将文本框,单选按钮和月份日历值插入到ms access 2010中 [英] Insert textbox, radio button and month calender values to ms access 2010 using VB.NET 2010
问题描述
亲爱的,
我想使用vb.net将文本框,单选按钮和月份日历值插入ms access 2010数据库。
我想清除所有文本框值,取消选中单选按钮并在将值插入数据库后设置月份日历的系统日期。
但它说INSERT INTO语句中存在语法错误。
请帮助我。
我尝试过:
Imports System.Data.OleDb
公共类dailyTrackingfrm
Dim Provider As String
Dim datafile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
-------------------------------------------------- -----------------
< pre> Private Sub dailyTrackingfrm_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
Provider =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =
datafile = D:\Tubewell \Tubewell \Tubewell.accdb
connString = Provider& datafile
myConnection.ConnectionString = connString
End Sub
------------------------------ -------------------------------------------------- ---
Private Sub cmdOk_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理cmdOk.Click
Dim res As String
Dim s As String
Dim obj As New DateTime()
如果RadioButton1.Checked = True则
s = RadioButton1.Text
Else
s = RadioButton2.Text
End if
res = MsgBox(你要添加这条记录吗?,vbQuestion + vbYesNo,添加记录)
如果res = vbYes那么
尝试
使用sqlconn作为新的OleDb。 OleDbConnection(Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\Tubewell \Tubewell \Tubewell.accdb)
使用sqlquery作为新的OleDb.OleDbCommand(INSERT INTO DailyTracking(EmpNo,类型,出勤率,日期)价值(?,?,?, ?),sqlconn)
sqlquery.Parameters.AddWithValue(@ EmpNo,txtempno.Text)
sqlquery.Parameters.AddWithValue(@ Type,txttype.Text)
sqlquery。 Parameters.AddWithValue(@ Attendance,s)
sqlquery.Parameters.AddWithValue(@ Date,MonthCalendar1.SelectionStart)
sqlconn.Open()
sqlquery.ExecuteNonQuery()
MsgBox(已成功插入记录,vbOKOnly,已插入记录)
txtempno.Text =
txttype.Text =
RadioButton1.Checked = False
RadioButton2.Checked = False
MonthCalendar1.TodayDate = obj
End using
End using
Catch ex As Exception
MessageBox.Show(ex.Message)
结束尝试
结束如果
结束子
结束类
你没有正确地声明参数在INSERT字符串中尝试;
使用sqlquery作为新的OleDb.OleDbCommand(INSERT INTO DailyTracking(EmpNo,Type,Attendance,Date)VALUES (@ EmpNo,@ Type,@ Attendance,@ Date),sqlconn)
旁注;对字段名称使用保留字不是一个好主意,例如,Date是一个变量类型,当使用像这样的名称时,阻止名称如此[Date],因此它被标识为不是变量类型的字段。
Dear all,
I want to insert textbox, radio button and Month calender values to ms access 2010 database using vb.net.
I want to clear the all the text box values, unchecked the radio buttons and set system date for month calender after insert the values to database.
But it says there is a syntax error at INSERT INTO statement.
Please help me on this.
What I have tried:
Imports System.Data.OleDb Public Class dailyTrackingfrm Dim Provider As String Dim datafile As String Dim connString As String Public myConnection As OleDbConnection = New OleDbConnection Public dr As OleDbDataReader ------------------------------------------------------------------- <pre> Private Sub dailyTrackingfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" datafile = "D:\Tubewell\Tubewell\Tubewell.accdb" connString = Provider & datafile myConnection.ConnectionString = connString End Sub ----------------------------------------------------------------------------------- Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click Dim res As String Dim s As String Dim obj As New DateTime() If RadioButton1.Checked = True Then s = RadioButton1.Text Else s = RadioButton2.Text End If res = MsgBox("Do you want to add this record?", vbQuestion + vbYesNo, "Add Record") If res = vbYes Then Try Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb") Using sqlquery As New OleDb.OleDbCommand("INSERT INTO DailyTracking(EmpNo,Type,Attendance,Date) VALUES (?,?,?,?)", sqlconn) sqlquery.Parameters.AddWithValue("@EmpNo", txtempno.Text) sqlquery.Parameters.AddWithValue("@Type", txttype.Text) sqlquery.Parameters.AddWithValue("@Attendance", s) sqlquery.Parameters.AddWithValue("@Date", MonthCalendar1.SelectionStart) sqlconn.Open() sqlquery.ExecuteNonQuery() MsgBox("Records inserted successfully", vbOKOnly, "Records inserted") txtempno.Text = "" txttype.Text = "" RadioButton1.Checked = False RadioButton2.Checked = False MonthCalendar1.TodayDate = obj End Using End Using Catch ex As Exception MessageBox.Show(ex.Message) End Try End If End Sub End Class
Your not declaring the parameters correctly in the INSERT string try;
Using sqlquery As New OleDb.OleDbCommand("INSERT INTO DailyTracking(EmpNo,Type,Attendance,Date) VALUES (@EmpNo,@Type,@Attendance,@Date)", sqlconn)
side note; It is not a good idea to use reserved words for field names, Date is a variable type for instance, when using names like this block the name like so [Date] so it is identified as a field not a variable type.
这篇关于使用VB.NET 2010将文本框,单选按钮和月份日历值插入到ms access 2010中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!