我試圖從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();
程序更新不拋出異常,或給我任何錯誤,數據庫根本不會改變:)
我希望有人能幫助我! :)
SQLReader的自IDisposable派生,請確保您使用的是'using'條款或您的一次性處置。 –
我做了一個代表簡單數據訪問層的類http://stackoverflow.com/questions/25816609/checking-user-name-or-user-email-already-exists/25817145#25817145這裏。目前你的代碼有點亂。如果你想要,你可以檢查它。您的代碼中存在多個問題,例如僅使用一個連接,不處理資源,我也沒有看到關閉連接的位置。 – mybirthname
如果您在management studio中使用相同的參數值執行結果查詢,您會看到什麼? – SpaceghostAli