2014-09-26 31 views
0

我有兩個表,ProjectsProjectImages。我想從第二個表中獲取image_id。這是我當前的代碼:我想在兩個表中更新一個按鈕button1_click

SqlConnection con = new SqlConnection(「Data Source = .; Initial Catalog = Waves; Integrated Security = true;」);

SqlCommand com = new SqlCommand("Update_Project", con); 
    com.CommandType = CommandType.StoredProcedure; 
    com.Parameters.AddWithValue("@ProjectID",Label1.Text); 
    com.Parameters.AddWithValue("@ProjectName",txtName.Text); 
    com.Parameters.AddWithValue("@VideoUrl", txtName.Text); 
    com.Parameters.AddWithValue("@Notes", txtName.Text); 
    com.Parameters.AddWithValue("@Date",DateTime.Now.Date); 
    WavesEntities wee = new WavesEntities(); 
    var query = from p in wee.Project_Images 
       select p.img_Id; 
    SqlCommand com1 = new SqlCommand("UpdateProjectImages", con); 
    com.CommandType = CommandType.StoredProcedure; 


    com.Parameters.AddWithValue("@img_id", query.AsEnumerable()); 
    com.Parameters.AddWithValue("@img_path", con); 
    con.Open(); 
    com.ExecuteNonQuery(); 
    con.Close(); 

錯誤:

No mapping exists from object type System.Data.Objects.ObjectQuery`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.

+0

你可以在一個存儲過程中做兩個更新嗎? – Tim 2014-09-26 15:49:57

+0

我無法獲取img_id參數 – 2014-09-26 15:55:07

+0

'com.Parameters.AddWithValue(「@ img_id」,query.AsEnumerable());'是問題所在,我不太確定你期望這樣做,但你正在嘗試以IEnumerable '作爲SQL參數。 – 2014-09-26 15:57:36

回答

0

編輯:看到你的編輯後,您的問題可能是一個不同的問題,不過,你可以試試我的回答。

編輯2:你在命令兩個參數所引用的命令1.更改參數以這樣的:

com1.Parameters.AddWithValue("@img_id", query.AsEnumerable()); 
com1.Parameters.AddWithValue("@img_path", con); 

你的問題是你不能使用相同的SqlConnection兩個「活着」 SqlCommands。

更改您的代碼是這樣的:

using(SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Waves;Integrated Security=true;")) 
{ 
    con.Open(); 

    using(SqlCommand com = new SqlCommand("Update_Project", con)) 
    { 
     com.CommandType = CommandType.StoredProcedure; 
     com.Parameters.AddWithValue("@ProjectID",Label1.Text); 
     com.Parameters.AddWithValue("@ProjectName",txtName.Text); 
     com.Parameters.AddWithValue("@VideoUrl", txtName.Text); 
     com.Parameters.AddWithValue("@Notes", txtName.Text); 
     com.Parameters.AddWithValue("@Date",DateTime.Now.Date); 
     WavesEntities wee = new WavesEntities(); 
     var query = from p in wee.Project_Images 
        select p.img_Id; 
    } 

    using(SqlCommand com1 = new SqlCommand("UpdateProjectImages", con)) 
    { 
     com.CommandType = CommandType.StoredProcedure; 


     com.Parameters.AddWithValue("@img_id", query.AsEnumerable()); 
     com.Parameters.AddWithValue("@img_path", con); 
     con.Open(); 
     com.ExecuteNonQuery(); 
    } 
} 
+0

問題是什麼我可以得到Img_id但incom.Parameters.AddWithValue(「@ img_id」,query.AsEnumerable()); – 2014-09-26 16:07:33

+0

繼@Ben Robinson的評論之後,您無法將query.AsEnumerable()作爲參數的值。如果你的意圖是傳遞一個整數值作爲參數值,你可以試試這個:Convert.ToInt32(query)。 – scubaFun 2014-09-26 16:13:58

+0

無法投射「System.Data.Objects.ObjectQuery'1 [System.Int32]」類型的對象以鍵入「System.IConvertible」。還沒有工作這個錯誤 – 2014-09-26 16:20:51

0

我認爲你需要從wee.Project_Images方法獲得img_id爲各自ProjectID,然後單獲取的值(img_id)傳遞到用於更新存儲過程。所以,你的query將成爲

var query1 = from p in wee.Project_Images 
       where p.ProjectID == Label1.Text 
       select p.img_id; 

com.Parameters.AddWithValue("@img_id", query); 

請注意,你不能傳遞一個Enumerable()對象作爲參數傳遞給存儲過程。同樣不要忘記使用using對於SqlConnectionSqlCommand對象的聲明,以便在資源被使用後更好地處理資源。

+0

這就是我想要的,但仍然不起作用 – 2014-09-26 19:22:02

+0

調試並檢查'var query'的結果以及作爲'img_id'參數傳入的內容。 – 2014-09-26 20:44:05

相關問題