在一个字段中插入多条记录
Posted
技术标签:
【中文标题】在一个字段中插入多条记录【英文标题】:insert multiple records in one field 【发布时间】:2015-07-13 02:40:08 【问题描述】:我有一个写在 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
这可能吗?
提前致谢!
【问题讨论】:
因此,如果我理解正确,您想在数据库中为每个选定的日期添加一个新行。在每一行中,除了天值之外,所有字段值都保持不变?如果是这种情况,那么有一个快速的代码解决方案。 嗨,下拉列表 DAYS (Days = DropDownList6.SelectedValue),允许多选。我需要将所有选定的值插入数据库的同一字段中。 IE 如果用户选择 1,2,3,4... 目前只添加第一个值 (1)。 【参考方案1】:这将为列表框中的每个选定项目添加一行,同时保留其他框的值。 遍历选定的日期并执行每一天的查询。
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
【讨论】:
没用。 “/”应用程序中的服务器错误。 INSERT INTO 查询不能包含多值字段。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.Data.OleDb.OleDbException:INSERT INTO 查询不能包含多值字段。源错误:第 48 行:cmd.Parameters.AddWithValue("@Emp", Emp) 第 49 行:第 50 行:cmd.ExecuteNonQuery() 第 51 行:下一个 这个参数有问题吗?第 48 行:cmd.Parameters.AddWithValue("@Emp", Emp) 我不确定这个错误来自哪里。这里的人可能会认出它并能够更好地解决您的问题。 不,错误是当我选择多条记录写入 accdb 文件异常详细信息:System.Data.OleDb.OleDbException:INSERT INTO 查询不能包含多值字段。如果我选择一条记录,则应用程序运行良好以上是关于在一个字段中插入多条记录的主要内容,如果未能解决你的问题,请参考以下文章