如何解决:UPDATE语句与FOREIGN KEY SAME TABLE约束冲突

时间:2019-05-23 11:54:32

标签: c# sql-update

我正在根据我的C#代码进行更新。现在我可以使用它了,但是我需要在类中添加一个构造函数。现在,我添加了构造函数,它将不再起作用。

如果我不做建筑就可以了

namespace FontysHotel
{
    public class Medewerker
    {
        //instantie variabelen
        private int id;
        private string naam;
        private string functie;
        private DateTime datum_in_dienst;
        private string telefoonnummer;
        private string email;
        private string wachtwoord;
        private int hotelid;
        private int managementid;

        //proporties
        public int Id
        {
            get { return id; } set { id = value; }
        }
        public string Naam
        {
            get { return naam; } set { naam = value; }
        }
        public string Functie
        {
            get { return functie; } set { functie = value; }
        }
       public DateTime Datum_in_dienst
        {
            get { return datum_in_dienst; } set { datum_in_dienst = value; }
        }
        public string Telefoonnummer
        {
            get { return telefoonnummer; } set { telefoonnummer = value; }
        }
        public string Email
        {
            get { return email; } set { email = value; }
        }
        public string Wachtwoord
        {
            get { return wachtwoord; } set { wachtwoord = value; }
        } 
        public int Hotelid
        {
            get { return hotelid; } set { hotelid = value; }
        }
        public int Managementid
        {
            get { return managementid; } set { managementid = value; }
        }
        //constructor
        public Medewerker(int id, string naam, string functie, DateTime datum_in_dienst, string telefoonnummer, string email, string wachtwoord, int hotelid, int mangementid)
        {
            Id = id;
            Naam = naam;
            Functie = functie;
            Datum_in_dienst = datum_in_dienst;
            Telefoonnummer = telefoonnummer;
            Email = email;
            Wachtwoord = wachtwoord;
            Hotelid = hotelid;
            Managementid = managementid;
        }


    }
}

方法

 public static void UpdateSchoonmaker(int id, string naam, string functie, DateTime datum_in_dienst, string telefoonnummer, string email,string wachtwoord, string hotelid, string managementid )
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("update medewerker set medewerkerid = " + id + ", naam ='" + naam + "',functie ='" + functie + "',datum_in_dienst = '" + datum_in_dienst + "',telefoonnummer =" + telefoonnummer + ",email ='" + email + "', wachtwoord ='" +wachtwoord+ "',hotelid = "+hotelid+", managementid ="+managementid+" where medewerkerid =" + id, conn);
                SqlDataReader reader;
                reader = cmd.ExecuteReader();
            }
        }

getallstaf中的构造函数

public static List<Medewerker> Getmedewerker()
        {
            var result = new List<Medewerker>();
            using (var conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                const string query = "Select * from medewerker where functie = 'schoonmaak'";
                SqlCommand selectMedewerker = new SqlCommand(query, conn);
                SqlDataReader reader = selectMedewerker.ExecuteReader();
                while (reader.Read())
                {
                    Medewerker mdw = new Medewerker((int)reader["medewerkerid"], (string)reader["naam"], (string)reader["functie"], (DateTime)reader["datum_in_dienst"], (string)reader["telefoonnummer"], (string)reader["email"], (string)reader["wachtwoord"], (int)reader["hotelid"], (int)reader["managementid"]);
                    //mdw.Id = (int)reader["medewerkerid"];
                    //mdw.Naam = (string)reader["naam"];
                    //mdw.Functie = (string)reader["functie"];
                    //mdw.Datum_in_dienst = (DateTime)reader["datum_in_dienst"];
                    //mdw.Telefoonnummer = (string)reader["telefoonnummer"];
                    //mdw.Email = (string)reader["email"];
                    //mdw.Wachtwoord = (string)reader["wachtwoord"];
                    //mdw.Hotelid = (int)reader["hotelid"];
                    //mdw.Managementid = (int)reader["managementid"];
                    result.Add(mdw);
                }
                reader.Close();
            }
            return result;
        }

从datagrid获取信息的代码

 private void DgSchoonmedewerker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dg = (DataGrid)sender;
            var row_selected = dg.SelectedItem as Medewerker;
            if (row_selected != null)
            {
                medewerkerid = row_selected.Id;
                tbNaamSchoon.Text = row_selected.Naam;
                tbTelefoonSchoon.Text = row_selected.Telefoonnummer;
                tbEmailSchoon.Text = row_selected.Email;
                tbWachtSchoon.Text = row_selected.Wachtwoord;
                cbHotelSchoonmaak.Text = row_selected.Hotelid.ToString();
                cbManagerSchoonmaak.Text = row_selected.Managementid.ToString();
                dpDatumindienst.SelectedDate = row_selected.Datum_in_dienst;
            }
        }

现在,如果我单击数据网格中的任何内容,它将信息填充到文本框中。下一个代码是我将代码发送到Update方法的方式

 Database.UpdateSchoonmaker(medewerkerid, tbNaamSchoon.Text, "Schoonmaak",Convert.ToDateTime(dpDatumindienst.SelectedDate), tbTelefoonSchoon.Text, tbEmailSchoon.Text, tbWachtSchoon.Text, cbHotelSchoonmaak.Text, cbManagerSchoonmaak.Text);

因此,首先,我使用公共静态列表getmedewerker加载信息。这可能是在managerids中加载错误。因为如您在第一个屏幕截图中所见,它们都被设置为0,而在sql server express中它们确实有一个值。 现在,如果我单击datagrid中的内容,managersid的combox也不会自动填充,如果我手动选择了一个managerid,则将0添加到managerid。 如果我不将任何东西放在新的medewerker中并取消注释mdw.XXX =并注释构造函数,则更新确实起作用

我希望它更新表中的一行。但是,如何将构造函数添加到类中并进行更新语句,以免出现此错误消息

编辑 enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我建议对面向对象的编程进行一些研究。我尚未对此进行测试,但它应该与我猜想的非常接近。

public class Medewerker
{
    //instantie variabelen
    public int id {get;}
    public string naam {get; set;}
    public string functie {get; set;}
    public DateTime datum_in_dienst {get; set;}
    public string telefoonnummer {get; set;}
    public string email {get; set;}
    public string wachtwoord {get; set;}
    public int hotelid {get; set;}
    public int managementid {get; set;}

    //constructor
    public Medewerker(int id, string naam, string functie, DateTime datum_in_dienst, string telefoonnummer, string email, string wachtwoord, int hotelid, int mangementid)
    {
        Id = id;
        Naam = naam;
        Functie = functie;
        Datum_in_dienst = datum_in_dienst;
        Telefoonnummer = telefoonnummer;
        Email = email;
        Wachtwoord = wachtwoord;
        Hotelid = hotelid;
        Managementid = managementid;
    }

    public static void UpdateSchoonmaker()
    {
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("update medewerker set naam ='" + this.naam + "',functie ='" + this.functie + "',datum_in_dienst = '" + this.datum_in_dienst + "',telefoonnummer =" + this.telefoonnummer + ",email ='" + this.email + "', wachtwoord ='" +this.wachtwoord+ "',hotelid = "+this.hotelid+", managementid ="+this.managementid+" where medewerkerid =" + id, conn);
            cmd.ExecuteNonQuery();
        }
    }
}

然后在您要调用该方法的任何区域

var myMedewerker = new Medewerker(0, "Matt", ...);
myMedewerker.naam = "James";
myMedewerker.UpdateSchoonmaker();