问题是,我怎样才能确保' product_category_id'在下拉列表中选择/没有选择后,ProductCategory被插入Product的product_category_id,同时显示ProductCategory的product_category_name? (FKS)
Dim sql2 As String =" INSERT INTO Product(product_category_id,product_name,product_title,product_desc,product_author,product_author_age,product_author_desc,product_other_detail,product_dimension1,product_dimension2,product_price,product_institution,product_status,product_delivery_time)VALUES(@product_category_id,@ product_name,@ product_title,@ product_desc,@ product_author,@ product_author_age,@ product_author_desc,@ product_other_detail,@ product_dimension1,@ product_dimension2,@ product_price,@ product_institution,@ product_status,@ product_delivery_time)"
cmd.CommandText = sql2 cmd.CommandType = CommandType.Text
'我认为以下陈述不正确?
cmd.Parameters.Add(New SqlParameter("@product_category_id", (ddlProductCategoryName2.selectedValue)))
cmd.Parameters.Add(New SqlParameter("@product_category_name", (ddlProductCategoryName2.SelectedValue)))
cmd.Parameters.Add(New SqlParameter("@product_name", (txtProductName2.Text)))
cmd.Parameters.Add(New SqlParameter("@product_title", (txtProductTitle2.Text)))
cmd.Parameters.Add(New SqlParameter("@product_desc", (txtProductDescription2.Text)))
cmd.Parameters.Add(New SqlParameter("@product_author", (txtProductAuthor2.Text)))
cmd.Parameters.Add(New SqlParameter("@product_author_age", (ddlProductAuthorAge2.SelectedValue)))
cmd.Parameters.Add(New SqlParameter("@product_author_desc", (txtProductAuthorDesc2.Text)))
答案 0 :(得分:1)
据我所知,没有理由你的SQL / VB不能按要求工作,你只需稍微更改下拉列表中的数据绑定即可。在页面加载方法中调用类似于以下内容的东西(我必须假设您的列名称):
dim adapter as new SqlDataAdapter("SELECT * FROM ProductCategory", [connectionstring])
dim table as new DataTable()
adapter.fill(table)
ddlProductCategoryName2.DataSource = table
ddlProductCategoryName2.DataValueField = "Product_Category_ID"
ddlProductCategoryName2.DataTextField = "Product_Category_Name"
这意味着
ddlProductCategoryName2.selectedValue
将返回product_category_ID,而不是下拉列表中显示的名称。
答案 1 :(得分:0)
如果出于某种原因您不想将product_Category_ID数据绑定到下拉列表,我会发布其他答案的替代方法。将以下函数添加到页面后面的代码中。
Private Function GetProductCategoryID(ByVal productCategoryName As String) As Int32
Dim id As Int32
Dim connectionString As String = "" 'your connection string
Using connection As New SqlConnection(connectionString)
connection.Open()
Using Adapter As New SqlDataAdapter("SELECT Product_Category_ID FROM ProductCategory WHERE Product_Category_Name = @Name", connection)
Dim table As New DataTable()
Adapter.SelectCommand.Parameters.AddWithValue("@Name", productCategoryName)
Adapter.Fill(table)
If (table.Rows.Count = 0) Then
Using command As New SqlCommand("INSERT ProductCategory (Proudct_Category_Name) VALUES (@Name) SELECT SCOPE_IDENITTY()", connection)
command.Parameters.AddWithValue("@Name", productCategoryName)
id = Convert.ToInt32(command.ExecuteScalar())
End Using
Else
id = Convert.ToInt32(table.Rows(0).Item(0))
End If
End Using
connection.Close()
End Using
Return id
End Function
然后你只需要更改当前代码中的一行:
cmd.Parameters.Add(New SqlParameter("@product_category_id", (ddlProductCategoryName2.selectedValue)))
到
cmd.Parameters.Add(New SqlParameter("@product_category_id", GetProductCategoryID(ddlProductCategoryName2.selectedValue)))
没有理由这个函数背后的逻辑不能完全在SQL中完成(即如果表中存在所选字符串的条目使用ID,否则插入它并获取ID),但我已经给定SQL解决方案here,我想这次我会给VB解决方案。