winfom练习

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

using System.IO;
namespace WindowsFormsApplication1
{
    public partial class demo : Form
    {
        public demo()
        {
            InitializeComponent();
        }

        SqlDataAdapter sda = null;
        DataTable dt = null;

        //加载
        private void demo_Load(object sender, EventArgs e)
        {
            this.txtid.Enabled = false;
            this.dataGridView1.AutoGenerateColumns = false;
            SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database= netshop_ceshi");
            sda = new SqlDataAdapter("select * from  userinfo", conn);

            dt = new DataTable();

            sda.Fill(dt);

            this.dataGridView1.DataSource = dt;
        }

        //数据绑定
        public void bind() {

            this.dataGridView1.AutoGenerateColumns = false;
            SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database= netshop_ceshi");
            sda = new SqlDataAdapter("select * from  userinfo", conn);

            dt = new DataTable();

            sda.Fill(dt);

            this.dataGridView1.DataSource = dt;
        }

        //点击数据格子修改数据
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database= netshop_ceshi");
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.UpdateCommand = new SqlCommand("update userinfo  set username=@username,userpwd=@userpwd,dizhi=@dizhi  where userid=@userid ", conn);
            SqlParameter name = sda.UpdateCommand.Parameters.Add("@username", SqlDbType.VarChar);
            SqlParameter pwd = sda.UpdateCommand.Parameters.Add("@userpwd", SqlDbType.VarChar);
            SqlParameter dizhi = sda.UpdateCommand.Parameters.Add("@dizhi", SqlDbType.VarChar);
            SqlParameter uid = sda.UpdateCommand.Parameters.Add("@userid", SqlDbType.Int);
            name.SourceColumn = "username";
            pwd.SourceColumn = "userpwd";
            dizhi.SourceColumn = "dizhi";
            uid.SourceColumn = "userid";
            name.SourceVersion = DataRowVersion.Current;
            pwd.SourceVersion = DataRowVersion.Current;
            dizhi.SourceVersion = DataRowVersion.Current;
            uid.SourceVersion = DataRowVersion.Current;
            sda.Update(dt);
            this.dataGridView1.DataSource = dt;
        }

        //修改
        private void button1_Click(object sender, EventArgs e)
        {

            DialogResult drt = MessageBox.Show("确定修改客户信息吗?", " 修改提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (drt == DialogResult.Yes)
            {

                SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database= netshop_ceshi");
                conn.Open();
                SqlCommand cmd = new SqlCommand("update userinfo  set username  ='" + this.txtname.Text + "',  userpwd  ='" + this.txtpwd.Text + "',    dizhi='" + this.txtdz.Text + "'   where userid =" + this.txtid.Text + " ",conn);
                
                int dr = cmd.ExecuteNonQuery();

                MessageBox.Show("修改成功!数据库已经更新!");

                conn.Close();
                bind();

            }
            else
            {
                MessageBox.Show("您取消了该操作!");
            }
        }

        //值传到文本框
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtid.Enabled = false;
            this.txtid.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            this.txtname.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            this.txtpwd.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            this.txtdz.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }


       //导出记事本
        private void bt_txt_Click(object sender, EventArgs e)
        {

            this.dataGridView1.AutoGenerateColumns = false;
            SqlConnection conn = new SqlConnection("server=8KQCDK6WIOTOHDM\\SQLEXPRESS;integrated security=True;database= netshop_ceshi");
            sda = new SqlDataAdapter("select * from  userinfo", conn);
           dt = new DataTable();
            sda.Fill(dt);
            this.dataGridView1.DataSource = dt;
            StreamWriter sw = new StreamWriter("d:\\user.txt", true, Encoding.Default);
            sw.WriteLine( "编号" + ""+ "姓名" + "" + "密码" + "" + "地址" + "");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sw.WriteLine(dt.Rows[i][0].ToString() + " " + dt.Rows[i][1].ToString() + "" + dt.Rows[i][2].ToString() + " " + dt.Rows[i][3].ToString() + " ");
            }
            sw.Flush();
            sw.Close();
          
        }
        //导出Excel
        private void button3_Click(object sender, EventArgs e)
        {
           

 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Application.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel.Worksheet mysheet = (Microsoft.Office.Interop.Excel.Worksheet)app.Worksheets["Sheet1"];
            app.Visible = true;
            mysheet.Cells[1, 1].Value = "编号";
            mysheet.Cells[1, 2].Value = "姓名";
            mysheet.Cells[1, 3].Value = "密码";
            mysheet.Cells[1, 4].Value = "地址";
            for (int i = 1; i <= dt.Rows.Count; i++)
            {

              
                mysheet.Cells[i+1, 1] = dt.Rows[i - 1][0].ToString();
                mysheet.Cells[i+1, 2] = dt.Rows[i -1][1].ToString();
                mysheet.Cells[i+1, 3] = dt.Rows[i - 1][2].ToString();
                mysheet.Cells[i+1, 4] = dt.Rows[i - 1][3].ToString();
            }


        }

     
    }
}

 

 界面

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值