SQL语句工作正常,但它不适用于以下代码。你能帮忙的话,我会很高兴。我试图在两个列表框中使用数据传递给这里的参数。
string cs = "Data Source= $%^$# ;Initial Catalog=Blah;User ID=ID;Password=Pass";
SqlConnection cn = new SqlConnection(cs);
cn.Open();
SqlCommand cmd = new SqlCommand("if not exists (select companyid, reportdefid from companyreports with (nolock) where companyid = @companyid and reportdefid = @reportdefid) insert into companyreports (companyid, reportdefid, tab, listorder) values ( @companyid, @reportdefid, 999, 1)", cn);
cmd.Parameters.AddWithValue("@companyid", listCompanytoAddto.Items); // need correct value here
cmd.Parameters.AddWithValue("@reportdefid", listAddedReports.Items);
MessageBox.Show("Update is complete");
cn.Close();
答案 0 :(得分:0)
您可以通过列表框项目进行操作,直到找到所需内容,然后将该项目放入对象并传递该对象ID。
CompanyClass myCompany = null;
foreach(CompanyClass item in listCompanytoAddTo.Items)
{
if(item.someproperty == something.somePropertyYourLookingFor)
{
myCompany = item;
}
}
cmd.Parameters.AddWithValue("@companyid", myCompany.id);
Edit: Do that same code above basically but for your other listbox.
cmd.ExecuteNonQuery();
答案 1 :(得分:0)
我不是数据库专家,但可以' companyid'和' reportdefid'接受多个值?
listCompanytoAddto.Items
将是列表中所有项目的数组,您可能实际上只需要
listCompanytoAddto.SelectedItem.ToString()