2017-01-08 70 views
1

刪除這是我的Form1.cs的代碼從列表框和數據庫

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace VoetbalApp 
{ 
    public partial class VoetbalApp : Form 
{ 
    Administration admin = new Administration(); 

    public VoetbalApp() 
    { 
     InitializeComponent(); 

     LoadSpelers(); 
    } 


    public void LoadSpelers() 
    { 


     string filter = tbNaam.Text; 
     lbSpelers.Items.Clear(); 

     List<Speler> spelers = admin.GetAllSpelers(filter); 

     foreach (Speler s in spelers.Where(s => s.Name.Contains(filter))) 
     { 
      lbSpelers.Items.Add(s); 
     } 

    } 

    public bool ClubCorrect(string club) 
    { 

     if (club != "Unitas 59") 
     { 
      return false; 
     } 

     else 
     { 
      return true; 
     } 
    } 

    private void btAdd_Click(object sender, EventArgs e) 
    { 
     string spelerNaam = tbAddNaam.Text; 
     string spelerClub = tbAddClub.Text; 

     if (ClubCorrect(spelerClub)) 
     { 
      admin.AddSpeler(spelerNaam, spelerClub); 

      LoadSpelers(); 
     } 

     else 
     { 
      MessageBox.Show("De door u ingevoerde club is niet juist." 
       ); 
     } 
    } 

    private void btDelete_Click(object sender, EventArgs e) 
    { 
     int id; 
     Int32.TryParse(tbId.Text, out id); 

     bool verwijderenIsGelukt = admin.RemoveSpeler(id); 

     if (verwijderenIsGelukt) 
     { 
      LoadSpelers(); 
      MessageBox.Show("Speler met ID" + id + " is succesvol verwijderd."); 
      tbId.Text = ""; 
     } 

     else 
     { 
      MessageBox.Show("Er is iets mis gegaan bij het verwijderen van student met ID" + id + ".", "Verwijderen is mislukt.", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

    private void tbNaam_TextChanged(object sender , EventArgs e) 
    { 
     LoadSpelers(); 
    } 

    private void tbNaam_TextChanged_1(object sender, EventArgs e) 
    { 
     LoadSpelers(); 
    } 


} 
} 

我的第二個代碼Administration.cs

using System.Collections.Generic; 
using System.Data; 

// For SQL server operations 
using System.Data.SqlClient; 
using System.Windows.Forms; 

namespace VoetbalApp 
{ 

class Administration 
{ 


    public static string connectionString = @"Initial Catalog=Speler.mdf;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Application.StartupPath + @"\Speler.mdf;Integrated Security=True"; 
    public SqlConnection conn = new SqlConnection(connectionString); 






    List<Speler> spelers = new List<Speler>(); 


    /// <param name="name">De naam van de nieuwe speler.</param> 
    /// <param name="club">De club van de nieuwe speler.</param> 
    /// 


    public void AddSpeler(string name, string club) 
    { 

      conn.Open(); 
      Speler speler = new Speler(); 
      speler.Name = name; 
      speler.Club = club; 
      string query = @"INSERT INTO Speler (Name, Club) OUTPUT Inserted.ID 
       VALUES (@name, @club)"; 
      SqlCommand cmd = new SqlCommand(query, conn); 
      cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = speler.Name; 
      cmd.Parameters.Add("@club", SqlDbType.NVarChar).Value = speler.Club; 
      speler.ID = (int)cmd.ExecuteScalar(); 
      conn.Close(); 


    } 





    /// <param name="ID">Het ID van de speler die verwijderd moet worden.</param> 
    /// <returns>Geeft true terug bij succes, en anders false.</returns> 
    public bool RemoveSpeler(int ID) 
    { 
     int nrOfRowsAffected = 0; 


     conn.Open(); 
      foreach (Speler s in spelers) 
      { 

       if (s.ID == ID) 
       { 

        string query = "DELETE FROM Speler WHERE ID = " + ID; 
        SqlCommand cmd = new SqlCommand(query, conn); 


        nrOfRowsAffected = cmd.ExecuteNonQuery(); 
        spelers.Remove(s); 


        break; 
       } 
      } 


     conn.Close(); 
     return nrOfRowsAffected > 0; 
    } 


    /// <param name="filter">De filter is om spelers te zoeken van de database.</param> 
    /// <returns>Een lijst van spelers waarvoor de filter is toegepast.</returns> 
    public List<Speler> GetAllSpelers(string filter) 
    { 



     spelers = new List<Speler>(); 


     string query = "SELECT S.* FROM Speler S"; 



     if (filter != "") 
     { 
      query += " WHERE Name LIKE '%" + filter + "%' OR Club LIKE '%" + filter + "%'"; 
     } 



     conn.Open(); 
     SqlCommand cmd = new SqlCommand(query, conn); 


     using (SqlDataReader reader = cmd.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 

       Speler speler = new Speler(); 


       speler.ID = reader.GetInt32(0); 
       speler.Name = reader.GetString(1); 
       speler.Club = reader.GetString(2); 


       spelers.Add(speler); 
      } 
     } 


     conn.Close(); 
     return spelers; 

    } 
} 
} 

所以這個問題是在在administration.cs的RemoveSpeler和在form1.cs中的btDelete。感謝您抽出時間。
我主要代碼在我的文章,所以我需要添加更多的細節,或者我不能發佈它,所以.. 所以我想要做的事情是,我有一個文本框(tbID),我需要把數量Speler的ID,然後按下按鈕(btDelete),從列表框/數據庫中刪除帶有我輸入的ID的Speler。但由於某種原因,它只是沒有做任何事情,沒有任何錯誤或任何事情。 而這仍然不夠詳細,所以我仍然不能發佈,所以我只需要輸入更多的東西在這裏,我不知道還有什麼類型在這裏。

+0

您的第一行代碼不在灰色背景thingy中,因爲它必須以4個連續空格開頭才能被視爲代碼的一部分。 –

+0

另外,您應該使用參數而不是連接字符串來創建您的sql語句。閱讀關於SQL注入。 –

+0

從列表中刪除'speler'嗎? –

回答

1
using System; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication1 
{ 
    public static class admin 
    { 
     public static bool RemoveSpeller(int id, ListBox listbox) 
     { 

      if (listbox.Items.Contains(id.ToString())) 
      { 
       MessageBox.Show("Exists"); 
       listbox.Items.Remove(id.ToString()); 
       try 
       { 

        SqlConnection con = new SqlConnection(/*Connection string goes here*/); 

        con.Open(); 
        SqlCommand com = new SqlCommand("DELETE FROM Speler WHERE id = '" + id + "'", con); 

        com.ExecuteNonQuery(); 

        con.Close(); 
        return true; 

       } 
       catch (SqlException) 
       { 
        MessageBox.Show("Something went wrong while removing the item"); 
        return false; 

       } 
      } 
      else 
      { 
       MessageBox.Show("Wasn't found!"); 
       return false; 
      } 


     } 
    } 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 

      admin.RemoveSpeller(Convert.ToInt16(tbId.Text), yourlistboxname); 

     } 
    } 
} 
+1

函數返回布爾值'return nrOfRowsAffected> 0;'有問題。 –

+0

我有我的第一個代碼在不同的類,所以我當我嘗試你的代碼,並把我的其他類中的RemoveFromDataBase代碼和我的主代碼中的button1_click它給了我一個錯誤(listbox.Items.IndexOf(** id * *)));名稱'id'在此上下文中不存在,另一個名稱在if(** rowsaffected ** == 1),並且與id相同。 –

+0

好吧我分開了每個班級的代碼!重命名它所說的Class1與你的類以及所有控件和你的控件名稱! – HelloIT