1

I wonder how to set The Picture box, and it will display 2 Images. On Value True image number 1, on value False, image number 2. I'm a noob in programming... My Record name is "oddal" dataType is "bit"

Here is my Code

    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

1 に答える 1

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 と言う...

于 2013-03-11T19:17:26.457 に答える