WindowsFormsApplication

来源:互联网 发布:php和asp.net哪个好 编辑:程序博客网 时间:2024/05/21 10:31
 只是不想把我的实验科目永远只是停留在我的电脑上  如果你是学这行技术的可以借鉴也可以提出批评  如果你不是学这个的  也可以帮你看看windows桌面窗体软件的一些简单的原理  技术的天堂因青春的激情而狂热  动人的音符因小伙伴们的支持而跳动对于界面UI  微软那套是可以直接把控件拖出来就用  这里就不多说(这里是用vs2010开发)From1.csusing 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;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public void getConn()        {            SqlConnection sqlConn = null;            try            {                sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");                sqlConn.Open();                SqlCommand sqlCmd = new SqlCommand();                sqlCmd.Connection = sqlConn;                sqlCmd.CommandText = "select * from student";                SqlDataReader sqlReader = sqlCmd.ExecuteReader();                while (sqlReader.Read())                {                    listBox1.Items.Add(Convert.ToString(sqlReader["number"]) + Convert.ToString(sqlReader["name"]) + Convert.ToString(sqlReader["sex"]) + Convert.ToString(sqlReader["age"]) + Convert.ToString(sqlReader["special"]));                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                sqlConn.Close();            }        }        public void getExecuteReader()        {            SqlConnection sqlConn = null;            try            {                sqlConn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename = D:\\student.mdf; Integrated Security = True;Connect Timeout=30;User Instance=True");                sqlConn.Open();                SqlCommand sqlCmd = new SqlCommand();                sqlCmd.Connection = sqlConn;                string queryString1 = "Select * from student where number = '";                string searchNumberValue1 = textBox1.Text;                queryString1 += searchNumberValue1 + "'";                SqlCommand sqlCommand1 = new SqlCommand(queryString1, sqlConn);                SqlDataReader read1 = sqlCommand1.ExecuteReader();                while (read1.Read())                {                    textBox2.Text= Convert.ToString(read1["name"]);                    textBox3.Text = Convert.ToString(read1["sex"]);                    textBox4.Text = Convert.ToString(read1["age"]);                    textBox5.Text = Convert.ToString(read1["special"]);                }                read1.Close();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                sqlConn.Close();            }        }        private void Form1_Load(object sender, EventArgs e)        {                    }        private void button1_Click(object sender, EventArgs e)        {            getExecuteReader();        }        private void button2_Click(object sender, EventArgs e)        {            getConn();        }        private void button3_Click(object sender, EventArgs e)        {            textBox1.Text = "";            textBox2.Text = "";            textBox3.Text = "";            textBox4.Text = "";            textBox5.Text = "";        }        private void button4_Click(object sender, EventArgs e)        {            //listBox1.Items.RemoveAt(0);//删一个下标为零的数据            //listBox1.Items.Remove();//给定要删源数据            listBox1.Items.Clear();            listBox1.Refresh();        }    }}

我们运行程序后点击测试数据按钮  测试我们的数据库中的数据已经添加进来


测试清空数据按钮


我们的student数据库中的数据


我们再查找一组数据


0 0
原创粉丝点击