11.20,winfrom,增加,查询,删除,修改

winfrom增加

 

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new   
    SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "insert into fruit     values(@ids,@name,@price,@source,@stack,@numbers,@image)";
    cmd.Parameters.Add("@ids",txtids.Text);
    cmd.Parameters.Add("@name", txtname.Text);
        cmd.Parameters.Add("@price", txtprice.Text);
    cmd.Parameters.Add("@source", txtsource.Text);
    cmd.Parameters.Add("@stack", txtstack.Text);
    cmd.Parameters.Add("@numbers", txtnumbers.Text);
    cmd.Parameters.Add("@image", txtimage.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

 

winfrom 查询

 

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new     
    SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "select *from fruit where name like     '%"+textBox1.Text.Trim()+"%' or ids like '%"+textBox1.Text.Trim()+"%'";
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    listView1.Items.Clear();
    while (dr.Read())
{
    ListViewItem lvt = new ListViewItem();
    lvt.Text = dr["ids"].ToString();//第一列
    lvt.SubItems.Add(dr["name"].ToString());
    lvt.SubItems.Add(dr["price"].ToString());
    lvt.SubItems.Add(dr["source"].ToString());
    lvt.SubItems.Add(dr["stack"].ToString());
    lvt.SubItems.Add(dr["numbers"].ToString());
    lvt.SubItems.Add(dr["image"].ToString());
    listView1.Items.Add(lvt); 
}
    conn.Close();
}

 

winfrom 删除:

 

private void button2_Click(object sender, EventArgs e)
{
//selecteditems存着选中项的集合
if (listView1.SelectedItems.Count > 0)
{
    foreach (ListViewItem lvt in listView1.SelectedItems)
{
//连接数据库,根据编号删除数据
    SqlConnection conn = new 
    SqlConnection("server=.;database=mydb;user=sa;pwd=123");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "delete from fruit where ids=@ids";
    cmd.Parameters.Clear();
    cmd.Parameters.Add("@ids",lvt.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

}
}
}

 

 

winfrom 修改

 

private void button1_Click(object sender, EventArgs e)
{
    FruitData data = new FruitData();
    data.Ids = txtids.Text;
    data.Name = txtname.Text;
    data.Price = decimal.Parse(txtprice.Text);
    data.Source = txtsource.Text;
    data.Stack = txtstack.Text;
    data.Image = txtimage.Text;
    data.Numbers = int.Parse(txtnumbers.Text);

    new FruitDA().Update(data);
} 

  

posted @ 2015-12-28 16:02  jlhea  阅读(277)  评论(0编辑  收藏  举报