我想從SQL數據庫中使用簡單的SELECT語句檢索一些行並將它們顯示在數據網格中,現在我所要做的就是將檢索到的值乘以一些因子然後顯示它。我試圖通過以下方式實現它:在C#中使用SQL select語句的幫助
我已經聲明PerDoseSize1是一個雙變量,它從函數中獲取它的值。我無法這樣做。 它給了我一個錯誤說「PerDoseSize1是不是一個有效的列」
public void FillDG1(string Chemical_Name0, string Chemical_Name1, string Chemical_Name2, string Chemical_Name3,double PerDoseSize1)
{
objDs.Clear();
string connString ="Data Source=dk1;Integrated Security=True";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT [Chemical Name],([GWP])*(perdosesize) AS GlobalWarming, ([ODP])*(perdosesize) AS OzoneDepletion, [WDP] AS WaterDepletion ,[FDP] AS FossilDepletion FROM [Surfactants$] WHERE ([Chemical Name] IN (@ChemicalName0, @ChemicalName1,@ChemicalName2 ,@ChemicalName3)) ";
cmd.Parameters.AddWithValue("@ChemicalName0",Chemical_Name0);
cmd.Parameters.AddWithValue("@ChemicalName1", Chemical_Name1);
cmd.Parameters.AddWithValue("@ChemicalName2", Chemical_Name2);
cmd.Parameters.AddWithValue("@ChemicalName3", Chemical_Name3);
cmd.Parameters.AddWithValue("@perdosesize", PerDoseSize1);
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
dAdapter.Fill(objDs);
DataTable myDataTable = objDs.Tables[0];
DG1.DataContext = objDs.Tables[0].DefaultView;
cmd.ExecuteNonQuery();
MessageBox.Show(ChemicalName0,ChemicalName1);
con.Close();
}
它似乎仍然沒有工作,這還算是錯了嗎?請幫忙!
感謝您的回覆。 – MangoTable 2011-03-23 18:33:59