1

我想知道如何设置图片框,它会显示 2 张图片。值为 True 图像编号 1,值为 False,图像编号 2。我是编程的菜鸟......我的记录名称是“奇数”数据类型是“位”

这是我的代码

    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 回答