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;
            Console.WriteLine("编号" + "" +"姓名" + "" + "密码" + "" + "地址");
            for (int i = 1; i <= dt.Rows.Count; i++)
            {

                Console.WriteLine("编号" + "" + "姓名" + "" + "密码" + "" + "地址");
                mysheet.Cells[i, 1] = dt.Rows[i - 1][0].ToString();
                mysheet.Cells[i, 2] = dt.Rows[i - 1][1].ToString();
               mysheet.Cells[i, 3] = dt.Rows[i - 1][2].ToString();
              mysheet.Cells[i, 4] = dt.Rows[i - 1][3].ToString();
            }
        }

     
    }
}

 

 界面winfom练习

winfom练习,布布扣,bubuko.com

winfom练习

上一篇:Eclipse中Maven Module和Maven Project的区别


下一篇:Winform打砖块游戏制作step by step第一节---主界面搭建