• 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);
    } 
    

      

  • 相关阅读:
    软件测试理论基础
    使用Pycharm官方统计代码行插件统计代码总行数
    Jmeter的配置文件解析
    python异常整理
    python2与python3的区别
    tomcat的server.xml配置
    异常:Error response from daemon: conflict: unable to delete 6fa48e047721 (cannot be forced)
    前端 -- 定位和z-index
    前端 -- background
    前端 -- 超链接导航栏案例
  • 原文地址:https://www.cnblogs.com/jlhea/p/5082889.html
Copyright © 2020-2023  润新知