.net实验报告

源代码:

1

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;

namespace WindowsFormsApplication1

{

    public partial class LoginForm : Form

    {

        public LoginForm()

        {

            InitializeComponent();

        }

        String str = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

        SqlConnection conn = new SqlConnection();

        SqlDataReader result = null;

        public bool loginus(string un, string pw)

        {

            conn.ConnectionString = str;

            string sql = "select * from user1 where username='" + un + " 'and password='" + pw + "'";

            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Connection.Open();

            try

            {

                result = cmd.ExecuteReader();

            }

            catch (Exception)

            {

                MessageBox.Show("对?不?起e!ê?查¨¦询¡¥数ºy据Y失º¡ì败㨹!ê?", "提¬¨¢示º?");

            }

            if (result != null)

            {

                if (result.Read())

                {

                    return true;

                }

            }

            result.Close();

            cmd.Connection.Close();

            return false;

        }

        private void button1_Click(object sender, EventArgs e)

            //确¨¡¤定¡§按ã¡ä钮£¤单Ì£¤击¡Â事º?件t

        {

            string n = textBox1.Text.Trim();

            string p = textBox2.Text.Trim();

            if ((textBox1.Text != "") & (textBox2.Text != ""))

            {

                if (loginus(n, p) == false)

                {

                    MessageBox.Show("用®?户¡ì或¨°密¨¹码?错䨪");

                    return;

                }

                DialogResult = DialogResult.OK;

                //给?窗ä¡ã体¬?的Ì?dialogresult属º?性?赋3值¦Ì

                this.Hide();

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}

2:

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;

namespace WindowsFormsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        private void label3_Click(object sender, EventArgs e)

        {

        }

        private void 精?确¨¡¤查¨¦询¡¥AToolStripMenuItem_Click(object sender, EventArgs e)

        {

            if (comboBox1.Text == "")

            {

                MessageBox.Show("请?先¨¨选?择?查¨¦询¡¥方¤?式º?!ê?", "提¬¨¢示º?");

            }

            else

            {

                string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

                SqlConnection sqlConnection1 = new SqlConnection();

                sqlConnection1.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

                string sql = "select * from hyt2008 where " + comboBox1.Text + "='" + textBox1.Text + "'";

                SqlCommand sqlCommand1 = new SqlCommand();

                sqlCommand1.CommandText = sql;

                sqlCommand1.Connection = sqlConnection1;

                SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection1);

                DataSet ds = new DataSet();

                try

                {

                    sqlConnection1.Open();

                    da.Fill(ds);

                }

                catch (Exception)

                {

                    MessageBox.Show("对?不?起e!ê?精?确¨¡¤查¨¦找¨°失º¡ì败㨹!ê?", "提¬¨¢示º?");

                }

                finally

                {

                    sqlConnection1.Close();

                }

                dataGridView1.DataSource = ds.Tables[0].DefaultView;

            }

        }

        private void 模¡ê糊y查¨¦询¡¥BToolStripMenuItem_Click(object sender, EventArgs e)

        {

            if (comboBox1.Text == "")

            {

                MessageBox.Show("请?先¨¨选?择?查¨¦询¡¥方¤?式º?!ê?", "提¬¨¢示º?");

            }

            else

            {

                string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

                SqlConnection sqlConnection1 = new SqlConnection();

                sqlConnection1.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

                string sql = "select * from hyt2008 where" + comboBox1.Text + "like'" + textBox1.Text + "%'";

                SqlCommand sqlCommand1 = new SqlCommand();

                sqlCommand1.CommandText = sql;

                sqlCommand1.Connection = sqlConnection1;

                SqlDataAdapter da = new SqlDataAdapter(sql, sqlConnection1);

                DataSet ds = new DataSet();

                try

                {

                    sqlConnection1.Open();

                    da.Fill(ds);

                }

                catch (Exception)

                {

                    MessageBox.Show("对?不?起e!ê?模¡ê糊y查¨¦找¨°失º¡ì败㨹!ê?", "提¬¨¢示º?");

                }

                finally

                {

                    sqlConnection1.Close();

                }

                dataGridView1.DataSource = ds.Tables[0].DefaultView;

            }

        }

        private void 添¬¨ª加¨®HToolStripMenuItem_Click(object sender, EventArgs e)

        {

            string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

            SqlConnection con = new SqlConnection();

            con.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

            string cmdText = @"insert into hyt2008(拼¡ä音°?检¨¬索¡Â,汉o字Á?检¨¬索¡Â,备À?注Á¡é)values('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

            SqlCommand cmd = new SqlCommand(cmdText, con);

            try

            {

                con.Open();

                cmd.ExecuteNonQuery();

                qingkong();

            }

            catch (Exception)

            {

                MessageBox.Show("对?不?起e!ê?添¬¨ª加¨®数ºy据Y失º¡ì败㨹!ê?", "提¬¨¢示º?");

            }

            finally

            {

                con.Close();

            }

            shuaxin();

        }

        void qingkong()

        {

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";

        }

        void shuaxin()

        {

            string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

            SqlConnection con = new SqlConnection();

            con.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

            string cmdText = "select * from hyt2008";

            SqlDataAdapter da = new SqlDataAdapter(cmdText, con);

            DataSet ds = new DataSet();

            try

            {

                con.Open();

                da.Fill(ds);

            }

            catch (Exception)

            {

                MessageBox.Show("对?不?起e!ê?精?确¨¡¤查¨¦找¨°失º¡ì败㨹!ê?", "提¬¨¢示º?");

            }

            finally

            {

                con.Close();

            }

            dataGridView1.DataSource = ds.Tables[0].DefaultView;

        }

        private void 删¦?除yJToolStripMenuItem_Click(object sender, EventArgs e)

        {

            string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

            SqlConnection con = new SqlConnection();

            con.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

            MessageBox.Show("删¦?除y前¡ã请?查¨¦询¡¥");

            if (comboBox1.Text == "")

            {

                MessageBox.Show("请?先¨¨输º?入¨?删¦?除y的Ì?类¤¨¤型¨ª");

            }

            else

            {

                string cmdText = "delete hyt2008 where " + comboBox1.Text + "='" + textBox1.Text + "'";

                SqlCommand cmd = new SqlCommand(cmdText, con);

                try

                {

                    con.Open();

                    cmd.ExecuteNonQuery();

                    qingkong();

                }

                catch (Exception)

                {

                    MessageBox.Show("对?不?起e!ê?删¦?除y数ºy据Y失º¡ì败㨹!ê?", "提¬¨¢示º?");

                }

                finally

                {

                    con.Close();

                }

                shuaxin();

            }

        }

        private void 退ª?出?EToolStripMenuItem_Click(object sender, EventArgs e)

        {

            this.Close();

        }

        private void 修T改?KToolStripMenuItem_Click(object sender, EventArgs e)

        {

            string aa = @"Data Source=127.0.0.1;Initial Catalog=学¡ì生¦¨²;Integrated Security=True";

            SqlConnection con = new SqlConnection();

            con.ConnectionString = aa;       //设¦¨¨置?连¢?接¨®字Á?符¤?串ä?

            MessageBox.Show("修T改?前¡ã请?查¨¦询¡¥");

            if (comboBox1.Text == "")

            {

                MessageBox.Show("请?先¨¨输º?入¨?修T改?的Ì?类¤¨¤型¨ª");

            }

            else

            {

                string cmdText = "update hyt2008 set 备À?注Á¡é='" + textBox4.Text + "' where " + comboBox1.Text + "='" + textBox1.Text + "'";

                SqlCommand cmd = new SqlCommand(cmdText, con);

                try

                {

                    con.Open();

                    cmd.ExecuteNonQuery();

                    qingkong();

                }

                catch (Exception)

                {

                    MessageBox.Show("对?不?起e!ê?修T改?数ºy据Y失º¡ì败㨹!ê?", "提¬¨¢示º?");

                }

                finally

                {

                    con.Close();

                }

                shuaxin();

            }

        }

    }

}

实验结果:

相关推荐