需要将记录添加到tblMeetings表,然后从tblMeetings获取该ID列(PK,Int,no null)以将tblVotings表添加到Meeting_ID列(int,not null)。前2个查询代码块工作!最后一个受保护的子" ftrInsert_Click"我试图将参数从一个表传递到另一个表时,块不起作用。我没有收到错误,记录插入到tblMeetings但不插入tblVotings。 我的整个aspx.vb代码页:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class _NewMeetings
Inherits System.Web.UI.Page
Dim WithEvents ftrInsert As New Button
Protected Sub GridView2_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName = "ftrInsert" Then
'call the insert sub (sender is the gridview that called the rowCommand,
'UsersDS is the name of our sqlDataSource on the page
gvQuickInsert(sender, SqlDataSource2)
End If
End Sub
Protected Sub gvQuickInsert(ByRef gv As GridView, ByRef ds As SqlDataSource)
Dim paramsArr As ArrayList = New ArrayList
Dim controlsArr As ArrayList = New ArrayList
Dim col As Integer = 0
Dim footrow As GridViewRow = gv.FooterRow
Try
Dim ix As Integer = 0
For ix = 0 To gv.Columns.Count
Dim d As DataControlField = gv.Columns(ix)
If d.InsertVisible = True Then
If footrow.Cells(ix).Controls(1) IsNot Nothing Then
paramsArr.Add(d.SortExpression.ToString)
controlsArr.Add(footrow.Cells(ix).Controls(1))
End If
End If
Next
Catch ex As Exception
End Try
ds.InsertParameters.Clear()
Dim ii As Integer = 0
For Each contr As Object In controlsArr
If contr.GetType.Name.ToLower.Contains("textbox") Then
ds.InsertParameters.Add(paramsArr(ii), TryCast(contr, TextBox).Text)
ElseIf contr.GetType.Name.ToLower.Contains("dropdownlist") Then
ds.InsertParameters.Add(paramsArr(ii), _
TryCast(contr, DropDownList).SelectedValue)
ElseIf contr.GetType.Name.ToLower.Contains("checkbox") Then
If TryCast(contr, CheckBox).Checked Then
ds.InsertParameters.Add(paramsArr(ii), 1)
Else
ds.InsertParameters.Add(paramsArr(ii), 0)
End If
End If
ii = ii + 1
Next
ds.Insert()
gv.DataBind()
End Sub
Protected Sub ftrInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ftrInsert.Click
Dim strConnection As String = ConfigurationManager.ConnectionStrings("ServiceLine_CommitteeConnectionString").ConnectionString
Using con As New SqlConnection(strConnection)
'Dim cmd As New SqlCommand("INSERT INTO tblVotings VALUES (@
Dim Meeting_ID As New SqlParameter("@ID", SqlDbType.Int)
Dim Committee_ID As New SqlParameter("@Comm", SqlDbType.Int)
Dim Member_Name As New SqlParameter("@Member", SqlDbType.NVarChar)
'Dim con As New SqlConnection(strConnection)
Dim cmd As New SqlCommand("INSERT INTO tblVotings(Meeting_ID, Committee_ID, Member_Name) SELECT M.ID AS Meeting_ID, C.ID AS Committee_ID, CM.Full_Name AS Member_Name FROM tblCommitteeName AS C INNER JOIN tblMeetings AS M ON M.ContractCategory = C.Contract_Category AND M.Committee = C.Committee_Name INNER JOIN tblCommitteeMembers AS CM ON CM.Committee = M.Committee WHERE (M.ID IN (@ID)) and (C.ID IN (@Comm)) and (CM.Full_Name IN (@Member))", con)
Meeting_ID.Value = 1
Committee_ID.Value = 1
Member_Name.Value = 1
cmd.Parameters.Add(Meeting_ID)
cmd.Parameters.Add(Committee_ID)
cmd.Parameters.Add(Member_Name)
cmd.Connection = con
cmd.CommandType = CommandType.Text
If con.State <> ConnectionState.Open Then con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
If con.state = ConnectionState.Open Then con.Close()
End Using
End Sub
结束班
答案 0 :(得分:0)
在SQL语句中使用参数非常简单,但有一些规则需要记住。
最常见的是那些新参与SQL参数的人会遇到的三个问题。
以下是使用SQL参数的示例,您应该能够剖析以获得所需内容。
Dim strConnection As String = "ConnString"
Using con As New SqlConnection(strConnection)
Dim cmd As New SqlCommand("INSERT INTO tbl(Col1) VALUES (@p_Para1)", con)
Dim p As New SqlParameter("@p_Para1", SqlDbType.Int, 8)
p.value = 1
cmd.Parameters.Add(p)
cmd.CommandType = CommandType.Text
If con.State <> ConnectionState.Open Then con.Open()
cmd.ExecuteNonQuery()
If con.State = ConnectionState.Open Then con.Close()
End Using
您的第二个错误(“无效的列名称'Meeting_ID'。无效的列名'委员会_ID'。无效的列名'Member_Name'。”)我认为这是因为您设置了WHERE
子句的方式。
所以而不是:
INSERT INTO tblVotings(Meeting_ID, Committee_ID, Member_Name)
SELECT M.ID AS Meeting_ID, C.ID AS Committee_ID, CM.Full_Name AS Member_Name
FROM tblCommitteeName AS C INNER JOIN tblMeetings AS M ON M.ContractCategory = C.Contract_Category AND M.Committee = C.Committee_Name INNER JOIN tblCommitteeMembers AS CM ON CM.Committee = M.Committee
WHERE (Meeting_ID IN (@ID)) and (Committee_ID IN (@Comm)) and (Member_Name IN (@Member))
尝试:
INSERT INTO tblVotings(Meeting_ID, Committee_ID, Member_Name)
SELECT M.ID AS Meeting_ID, C.ID AS Committee_ID, CM.Full_Name AS Member_Name
FROM tblCommitteeName AS C INNER JOIN tblMeetings AS M ON M.ContractCategory = C.Contract_Category AND M.Committee = C.Committee_Name INNER JOIN tblCommitteeMembers AS CM ON CM.Committee = M.Committee
WHERE (M.ID IN (@ID)) and (C.ID IN (@Comm)) and (CM.Full_Name IN (@Member))