2013-03-11 20 views
1

我不知道如何設置The Picture框,它會顯示2個圖像。論價值真像1號,價值假,圖像號碼2我在編程菜鳥...我的記錄名稱是「oddal」數據類型是「位」如何在數據庫中將值設爲True或False時顯示圖像

這裏是我的代碼

namespace WindowsFormsApplication1 
    { 
public partial class Form1 : Form 
{ 
    SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MSI\Documents\Visual Studio 2010\Projects\Baza z własnymi batonami\Baza z własnymi batonami\Database1.mdf;Integrated Security=True;User Instance=True"); 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataReader dr; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     cmd.Connection = cn; 
     pokazliste(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (textid.Text != "" & textimie.Text != "") 
     { 
      cn.Open(); 
      cmd.CommandText = "Insert into Table1 (id,imie) values('" + textid.Text + "','" + textimie.Text + "')"; 
      cmd.ExecuteNonQuery(); 
      cmd.Clone(); 
      MessageBox.Show("Wpis Wprowadzony","Vindykacja by Brzoska"); 
      cn.Close(); 
      textid.Text = ""; 
      textimie.Text = ""; 
      pokazliste(); 
     } 
    } 

     private void pokazliste() 
     { 
      listBox1.Items.Clear(); 
      listBox2.Items.Clear(); 
      cn.Open(); 
      cmd.CommandText = "Select * From Table1"; 
      dr = cmd.ExecuteReader(); 
      if (dr.HasRows) 
      { 
       while(dr.Read()) 
       { 
        listBox1.Items.Add(dr[0].ToString()); 
        listBox2.Items.Add(dr[1].ToString()); 

       } 
      } 
      cn.Close(); 
     } 

     private void listBox2_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      ListBox l = sender as ListBox; 
      if (l.SelectedIndex != -1) 
      { 
       listBox1.SelectedIndex = l.SelectedIndex; 
       listBox2.SelectedIndex = l.SelectedIndex; 
       textid.Text = listBox1.SelectedItem.ToString(); 
       textimie.Text = listBox2.SelectedItem.ToString(); 

      } 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      if (textid.Text != "" & textimie.Text != "" & listBox1.SelectedIndex!=-1) 
      { 
       cn.Open(); 
       cmd.CommandText = "delete from Table1 where id='" + listBox1.SelectedItem.ToString() + "' and imie= '" + listBox2.SelectedItem.ToString() + "'"; 
       cmd.ExecuteNonQuery(); 
       cn.Close(); 
       MessageBox.Show("Wpis Uaktualniony", "Vindykacja by Brzoska"); 
       pokazliste(); 
       textid.Text = ""; 
       textimie.Text = ""; 

      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      if (textid.Text != "" & textimie.Text != "") 
      { 
       cn.Open(); 
       cmd.CommandText = "Update Table1 set id='" + textid.Text + "', imie= '" + textimie.Text + "'Where id'" + textid.Text + "'and imie= '" + textimie.Text + "'"; 
       cmd.ExecuteNonQuery(); 
       cn.Close(); 
       MessageBox.Show("Wpis Usuniety", "Vindykacja by Brzoska"); 
       pokazliste(); 
       textid.Text = ""; 
       textimie.Text = ""; 

      } 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 

     } 
} 
    } 
+4

首先,你的代碼是開放給SQL注入的。在查詢中使用參數。見[這篇文章](http://csharp-station.com/Tutorial/AdoDotNet/Lesson06) – 2013-03-11 16:42:21

+0

我的老師也這麼說,但他說得很好,可以。但如果我想通過,我需要把這張照片。 我閱讀這篇文章,但我真的不明白它... – user2157657 2013-03-11 17:04:18

回答

1
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\MSI\Documents\Visual Studio 2010\Projects\Baza z własnymi batonami\Baza z własnymi batonami\Database1.mdf;Integrated Security=True;User Instance=True"); 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataReader dr; 
    cn.Open(); 
    cmd.CommandText = "SELECT oddal FROM TableName WHERE (Your = Condition)"; 
    dr = cmd.ExecuteReader(); 
    if dr.Read() 
    { 
      if dr("oddal") 
      { 
      //Set picture box to image 1 
      } 
      else 
      { //Set picture box to image 1 
      } 
    } 
    cn.Close(); 

確保在SELECT中指定您的條件。此外,爲了將來,爲控件分配有意義的名稱。比如說button1,比如說btnInsert ...

相關問題