2016-09-27 22 views
0

SQL ServerテーブルをC#から更新しようとしています。私は今数時間テーブルを更新することに苦労しています。私はそんなに検索しましたが、何が間違っているのか頭を巡らすことはできません。私はその行を更新したいがある場合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を表すクラスを作成しました。現在、あなたのコードはちょっと混乱しています。もしあなたがそれを確認することができます。コード内に複数の問題があります。たとえば、接続を1つしか使用しない、リソースを破棄しない、接続をどこで閉じるかはわかりません。 – mybirthname

+2

同じスタジオで結果のクエリを同じパラメータ値で実行すると、何が表示されますか? – 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フォルダにコピーして、以前に行った変更を上書きするように見えます。そのため、変更が反映されませんでした。 –

関連する問題