2016-09-27 127 views
0

我試圖從C#更新我的SQL Server表。我一直在努力更新桌子幾個小時,現在我已經搜索了很多東西,我無法將自己的頭圍繞在錯誤的問題上。更新SQL Server表 - 表未更新

public class SQLConnect 
{ 
    public SQLConnect(string startUp) 
    { 
     startupPath = startUp; 
     connectionSuccesful = false; 
     OpenConnection(); 
    } 

    public SqlConnection sqlConnect; 
    public string startupPath { get; set; } 
    public bool connectionSuccessful { get; set; } 
    public bool temp { get; set; } 

    public void IndtastBeløb(int beløb, string kategori) 
    { 
     DateTime time = DateTime.Now; 
     int iBolig = 0; int iOther = 0; int iTransport = 0; int iLoan = 0; int iMad = 0; int iDiverse = 0; 
     SqlCommand command = new SqlCommand("SELECT * FROM Entries WHERE dag = @day AND måned = @month AND år = @year", sqlConnect); // 

     command.Parameters.AddWithValue("@day", time.Day); 
     command.Parameters.AddWithValue("@month", time.Month); 
     command.Parameters.AddWithValue("@year", time.Year); 

     /*command.Parameters.Add("@day", SqlDbType.Int); 
     command.Parameters["@day"].Value = time.Day; 
     command.Parameters.Add("@month", SqlDbType.Int); 
     command.Parameters["@month"].Value = time.Month; 
     command.Parameters.Add("@year", SqlDbType.Int); 
     command.Parameters["@year"].Value = time.Year;*/ 

     SqlDataReader reader = command.ExecuteReader(); 

     if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 
       //thing = reader["bolig"].ToString(); 
       //int iBolig = reader.GetInt32(0); 
       iBolig = (int)reader["bolig"]; 
       iOther = (int)reader["øvrige"]; 
       iTransport = (int)reader["transport"]; 
       iLoan = (int)reader["gæld"]; 
       iMad =(int)reader["mad"]; 
       iDiverse = (int)reader["diverse"];      

       switch (kategori) 
       { 
        case "Bolig": 
         iBolig += beløb; 
         break; 
        case "Øvrige": 
         iOther += beløb; 
         break; 
        case "Transport": 
         iTransport += beløb; 
         break; 
        case "gæld": 
         iLoan += beløb; 
         break; 
        case "mad": 
         iMad += beløb; 
         break; 
        case "diverse": 
         break; 
        default: 
         break; 
       } 
      } 

      reader.Close(); 

      SqlCommand changeRow = new SqlCommand("UPDATE Entries SET [email protected] WHERE [email protected]", sqlConnect); //, ø[email protected]øvrige, [email protected], gæ[email protected]æld, [email protected], [email protected] " + "WHERE [email protected] AND må[email protected] AND å[email protected]" 
      changeRow.Parameters.AddWithValue("@bolig", iBolig); 
      changeRow.Parameters.AddWithValue("@day", time.Day); 
      /*changeRow.Parameters.AddWithValue("@øvrige", iOther); 
      changeRow.Parameters.AddWithValue("@transport", iTransport); 
      changeRow.Parameters.AddWithValue("@gæld", iLoan); 
      changeRow.Parameters.AddWithValue("@mad", iMad); 
      changeRow.Parameters.AddWithValue("@diverse", iDiverse); 

      changeRow.Parameters.AddWithValue("@month", time.Month); 
      changeRow.Parameters.AddWithValue("@year", time.Year);*/ 

      changeRow.ExecuteNonQuery(); 
     } 
     else 
     { 
      temp = false; 
     } 
    } 

    public void OpenConnection() 
    { 
     sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='" + startupPath + @"LuksusDatabase.mdf';Integrated Security=True"); 
     //sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='c:\users\simo8211\documents\visual studio 2015\Projects\LuksusFældenForms\LuksusFældenForms\bin\Debug\LuksusDatabase.mdf';Integrated Security=True"); 

     try 
     { 
      sqlConnect.Open(); 
      connectionSuccessful = true; 
     } 
     catch 
     { 
      connectionSuccessful = false; 
     } 
    } 
} 

的重點是IndtastBeløb方法,即時試圖確定是否有數據庫的當前日期的條目,如果我想更新該行。

一切都在代碼工作,直到我試圖與

changeRow.ExecuteNonQuery(); 

程序更新不拋出異常,或給我任何錯誤,數據庫根本不會改變:)

我希望有人能幫助我! :)

+1

SQLReader的自IDisposable派生,請確保您使用的是'using'條款或您的一次性處置。 –

+0

我做了一個代表簡單數據訪問層的類http://stackoverflow.com/questions/25816609/checking-user-name-or-user-email-already-exists/25817145#25817145這裏。目前你的代碼有點亂。如果你想要,你可以檢查它。您的代碼中存在多個問題,例如僅使用一個連接,不處理資源,我也沒有看到關閉連接的位置。 – mybirthname

+2

如果您在management studio中使用相同的參數值執行結果查詢,您會看到什麼? – SpaceghostAli

回答

0

代碼沒有問題,似乎沒有匹配的更新條件行。

下面是測試的代碼工作正常;

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WindowsFormsApplication1 
{ 
    public class SQLConnect 
    { 
     public SQLConnect(string startUp) 
     { 
      startupPath = startUp; 
      connectionSuccesful = false; 
      OpenConnection(); 
     } 

     public SqlConnection sqlConnect; 
     public string startupPath { get; set; } 
     public bool connectionSuccesful { get; set; } 
     public bool temp { get; set; } 

     public void IndtastBeløb(int beløb, string kategori) 
     { 
      DateTime time = DateTime.Now; 
      int iBolig = 0; int iOther = 0; int iTransport = 0; int iLoan = 0; int iMad = 0; int iDiverse = 0; 
      SqlCommand command = new SqlCommand("SELECT * FROM Entries WHERE dag = @day AND maned = @month AND ar = @year", sqlConnect); // 

      command.Parameters.AddWithValue("@day", time.Day); 
      command.Parameters.AddWithValue("@month", time.Month); 
      command.Parameters.AddWithValue("@year", time.Year); 

      /*command.Parameters.Add("@day", SqlDbType.Int); 
      command.Parameters["@day"].Value = time.Day; 
      command.Parameters.Add("@month", SqlDbType.Int); 
      command.Parameters["@month"].Value = time.Month; 
      command.Parameters.Add("@year", SqlDbType.Int); 
      command.Parameters["@year"].Value = time.Year;*/ 

      SqlDataReader reader = command.ExecuteReader(); 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        //thing = reader["bolig"].ToString(); 
        //int iBolig = reader.GetInt32(0); 
        iBolig = (int)reader["bolig"]; 
        iOther = (int)reader["ovrige"]; 
        iTransport = (int)reader["transport"]; 
        iLoan = (int)reader["gold"]; 
        iMad = (int)reader["mad"]; 
        iDiverse = (int)reader["diverse"]; 

        switch (kategori) 
        { 
         case "Bolig": 
          iBolig += beløb; 
          break; 
         case "ovrige": 
          iOther += beløb; 
          break; 
         case "Transport": 
          iTransport += beløb; 
          break; 
         case "gold": 
          iLoan += beløb; 
          break; 
         case "mad": 
          iMad += beløb; 
          break; 
         case "diverse": 
          break; 
         default: 
          break; 
        } 
       } 
       reader.Close(); 

       SqlCommand changeRow = new SqlCommand("UPDATE Entries SET [email protected] WHERE [email protected]", sqlConnect); //, ø[email protected]øvrige, [email protected], gæ[email protected]æld, [email protected], [email protected] " + "WHERE [email protected] AND må[email protected] AND å[email protected]" 
       changeRow.Parameters.AddWithValue("@bolig", iBolig); 
       changeRow.Parameters.AddWithValue("@day", time.Day); 
       /*changeRow.Parameters.AddWithValue("@øvrige", iOther); 
       changeRow.Parameters.AddWithValue("@transport", iTransport); 
       changeRow.Parameters.AddWithValue("@gæld", iLoan); 
       changeRow.Parameters.AddWithValue("@mad", iMad); 
       changeRow.Parameters.AddWithValue("@diverse", iDiverse); 

       changeRow.Parameters.AddWithValue("@month", time.Month); 
       changeRow.Parameters.AddWithValue("@year", time.Year);*/ 

       int cc = changeRow.ExecuteNonQuery(); 
      } 
      else 
      { 
       temp = false; 
      } 

     } 


     public void OpenConnection() 
     { 
      sqlConnect = new SqlConnection(@"initial catalog=StackOverflow;User Id=sa;password=xxxxxx;Server=.;"); 
      //sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='c:\users\simo8211\documents\visual studio 2015\Projects\LuksusFældenForms\LuksusFældenForms\bin\Debug\LuksusDatabase.mdf';Integrated Security=True"); 
      try 
      { 
       sqlConnect.Open(); 
       connectionSuccesful = true; 
      } 
      catch 
      { 
       connectionSuccesful = false; 
      } 
     } 
    } 

} 


private void Form1_Load(object sender, EventArgs e) 
{ 
    SQLConnect ocon = new WindowsFormsApplication1.SQLConnect(Application.StartupPath); 

    if(ocon.connectionSuccesful) 
    { 
     ocon.IndtastBeløb(1, "Bolig"); 
    } 
} 

SQL腳本;

create table Entries (bolig int, dag int, maned int, ar int, ovrige int,transport int, gold int, mad int,diverse int) 

insert into Entries values(1,27,9,2016,1,2,3,4,5) 

-- after update 
select * from Entries 

結果

enter image description here

+0

哦,上帝,謝謝! 我一直在弄亂我的代碼,並沒有意識到發生了什麼事情!謝謝你證明代碼工作正常,否則我會在這裏坐幾天。似乎每次我調試程序時,Visual Studio都會將基本服務器複製到Debug文件夾並覆蓋之前所做的更改,這就是爲什麼我無法看到它所做的更改! –