更新SQL Server表 - 表未更新

时间:2016-09-27 16:10:12

标签: c# sql sql-server database

我正在尝试从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 bolig=@bolig WHERE dag=@day", sqlConnect); //, øvrige=@øvrige, transport=@transport, gæld=@gæld, mad=@mad, diverse=@diverse " + "WHERE dag=@day AND måned=@month AND år=@year"
            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 个答案:

答案 0 :(得分: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 bolig=@bolig WHERE dag=@day", sqlConnect); //, øvrige=@øvrige, transport=@transport, gæld=@gæld, mad=@mad, diverse=@diverse " + "WHERE dag=@day AND måned=@month AND år=@year"
                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