我有一个在accdb文件中写入的代码并且工作正常。我的经理向我请求DIAS字段(您将在代码中找到下面的字段),允许多次选择,在数据库中插入所有选定的值。现在它只插入第一个:
Imports System.Data.OleDb
Imports System.Data
Imports System.Collections.Generic
Imports System.Web.UI.WebControls.ListItem
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As OleDbConnection
Dim cmd As OleDbCommand
Dim sqlString, FName, LName, Account, Supervisor, Type, Justification, Days, Clock, Emp, Mess As String
FName = Name.Text
LName = Last.Text
Account = DropDownList1.SelectedValue
Supervisor = DropDownList2.SelectedValue
Type = DropDownList4.SelectedValue
Justification = Jus.Text
Clock = DropDownList3.SelectedValue
Days = DropDownList6.SelectedValue
Emp = DropDownList5.SelectedValue
Mess = ListBox1.SelectedValue
Response.Write("<script language=""javascript"">alert('¡Gracias por la información! Saludos!!');</script>")
myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WFM\Database4.accdb")
myConn.Open()
' sqlString = "INSERT INTO Absence (FName, LName, Account, Supervisor, Tipo, Justification, Clock, Cuando) VALUES ( " + FName + "," + LName + "," + Account + "," + Supervisor + "," + Type + "," + Justification + "," + Clock + "," + Days + ")"
sqlString = "INSERT INTO Absence (FName, LName, Account, Supervisor, Tipo, Justification, Clock, Diass, Mess, Emp) VALUES ( @FName, @LName, @Account, @Supervisor, @Type, @Justification, @Clock, @Days, @Mess, @Emp)"
cmd = New OleDbCommand(sqlString, myConn)
cmd.Parameters.AddWithValue("@FName", FName)
cmd.Parameters.AddWithValue("@LName", LName)
cmd.Parameters.AddWithValue("@Account", Account)
cmd.Parameters.AddWithValue("@Supervisor", Supervisor)
cmd.Parameters.AddWithValue("@Tipo", Type)
cmd.Parameters.AddWithValue("@Justification", Justification)
cmd.Parameters.AddWithValue("@Clock", Clock)
cmd.Parameters.AddWithValue("@Days", Days)
cmd.Parameters.AddWithValue("@Mess", Mess)
cmd.Parameters.AddWithValue("@Emp", Emp)
cmd.ExecuteNonQuery()
myConn.Close()
End Sub
这可能吗?
提前致谢!
答案 0 :(得分:0)
这将为列表框中的每个选定项添加一行,同时保留其他框的值。 循环选定的日期并执行每天的查询。
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myConn As OleDbConnection
Dim cmd As OleDbCommand
Dim sqlString, FName, LName, Account, Supervisor, Type, Justification, Days, Clock, Emp, Mess As String
FName = Name.Text
LName = Last.Text
Account = DropDownList1.SelectedValue
Supervisor = DropDownList2.SelectedValue
Type = DropDownList4.SelectedValue
Justification = Jus.Text
Clock = DropDownList3.SelectedValue
Emp = DropDownList5.SelectedValue
Mess = ListBox1.SelectedValue
Response.Write("<script language=""javascript"">alert('¡Gracias por la información! Saludos!!');</script>")
myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WFM\Database4.accdb")
myConn.Open()
' sqlString = "INSERT INTO Absence (FName, LName, Account, Supervisor, Tipo, Justification, Clock, Cuando) VALUES ( " + FName + "," + LName + "," + Account + "," + Supervisor + "," + Type + "," + Justification + "," + Clock + "," + Days + ")"
sqlString = "INSERT INTO Absence (FName, LName, Account, Supervisor, Tipo, Justification, Clock, Diass, Mess, Emp) VALUES ( @FName, @LName, @Account, @Supervisor, @Type, @Justification, @Clock, @Days, @Mess, @Emp)"
for each s as string in DropDownList6.selectedItems
Days = s
cmd = New OleDbCommand(sqlString, myConn)
cmd.Parameters.AddWithValue("@FName", FName)
cmd.Parameters.AddWithValue("@LName", LName)
cmd.Parameters.AddWithValue("@Account", Account)
cmd.Parameters.AddWithValue("@Supervisor", Supervisor)
cmd.Parameters.AddWithValue("@Tipo", Type)
cmd.Parameters.AddWithValue("@Justification", Justification)
cmd.Parameters.AddWithValue("@Clock", Clock)
cmd.Parameters.AddWithValue("@Days", S)
cmd.Parameters.AddWithValue("@Mess", Mess)
cmd.Parameters.AddWithValue("@Emp", Emp)
cmd.ExecuteNonQuery()
Next
myConn.Close()
End Sub
编辑:我忘了将从Days传递的变量更改为S.然后代码现在反映了这些更改。
另请参阅此帖子http://www.access-programmers.co.uk/forums/showthread.php?t=233106