本文介绍了如何在组合框中检索所选项目的主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 SqlDataAdapter adapter2 = new SqlDataAdapter( SELECT * FROM dbo.OrderLines,cn); adapter2.InsertCommand = new SqlCommand( dbo.InsertOrderLine,cn); adapter2.InsertCommand.CommandType = CommandType.StoredProcedure; adapter2.InsertCommand.Parameters.Add( new SqlParameter( @ ServiceProviderID,SqlDbType.Int, 15 , ServiceProviderID)); adapter2.InsertCommand.Parameters.Add( new SqlParameter( @ ProductID,SqlDbType.Int, 15 , 产品ID)); adapter2.InsertCommand.Parameters.Add( new SqlParameter( @ PolicyNumber,SqlDbType.NVarChar, 50 , PolicyNumber)); SqlParameter parameter2 = adapter.InsertCommand.Parameters.Add( @ ID2,SqlDbType.Int, 0 , OrderLineID); parameter2.Direction = ParameterDirection.Output; DataTable orderline = new DataTable(); adapter.Fill(orderline); // 从Products表中查找productComboBox.SelectedItem的主键 // 和来自ServiceProviders表的serviceProviderCombobox.SelectedItem DataRow newRow2 = orderline.NewRow (); newRow2 [ ServiceProviderID] =?serviceProviderComboBox.SelectedItem?; newRow2 [ ProductID] =?productComboBox.SelectedItem?; newRow2 [ PolicyNumber] = policyTextBox.Text; orderline.Rows.Add(newRow2); adapter.Update(orderline); id2 = int .Parse(parameter2.Value.ToString()); 解决方案 意识到SelectedValue已包含主键。将它分配给变量。 添加以下两行: int spid = int.Parse(serviceProviderComboBox .SelectedValue.ToString()); int pid = int.Parse(productComboBox.SelectedValue.ToString()); 你必须编写一个选择查询返回主键 query =从表中选择combobox.selectedvalue(或选定文本)。(yourtable name) 其中primarykey = XY或其他 SqlDataAdapter adapter2 = new SqlDataAdapter("SELECT * FROM dbo.OrderLines", cn);adapter2.InsertCommand = new SqlCommand("dbo.InsertOrderLine", cn);adapter2.InsertCommand.CommandType = CommandType.StoredProcedure;adapter2.InsertCommand.Parameters.Add(new SqlParameter("@ServiceProviderID", SqlDbType.Int, 15, "ServiceProviderID"));adapter2.InsertCommand.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.Int, 15, "ProductID"));adapter2.InsertCommand.Parameters.Add(new SqlParameter("@PolicyNumber", SqlDbType.NVarChar, 50, "PolicyNumber"));SqlParameter parameter2 = adapter.InsertCommand.Parameters.Add("@ID2", SqlDbType.Int, 0, "OrderLineID");parameter2.Direction = ParameterDirection.Output;DataTable orderline = new DataTable();adapter.Fill(orderline);// find primary key of both productComboBox.SelectedItem from Products table// and serviceProviderCombobox.SelectedItem from ServiceProviders tableDataRow newRow2 = orderline.NewRow();newRow2["ServiceProviderID"] = ?serviceProviderComboBox.SelectedItem?;newRow2["ProductID"] = ?productComboBox.SelectedItem?;newRow2["PolicyNumber"] = policyTextBox.Text;orderline.Rows.Add(newRow2);adapter.Update(orderline);id2 = int.Parse(parameter2.Value.ToString()); 解决方案 Realised that the "SelectedValue" already contained the primary key. Assigned it to variables.Added these two lines:int spid = int.Parse(serviceProviderComboBox.SelectedValue.ToString());int pid = int.Parse(productComboBox.SelectedValue.ToString());you must write a select query that return primary keyquery= select combobox.selectedvalue(or selected text) from table.(yourtable name)where primarykey= X Y or else 这篇关于如何在组合框中检索所选项目的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 10:20