c#.net用户登录、注册和模糊查询源代码

c#.net用户登录、注册和模糊查询源代码

登陆源代码:    

   private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名和密码,然后再登陆");
                return;
            }
            string SQL = "select 类别 from 用户表 where 用户名=";
            SQL += "'" + textBox1.Text + "'" + "and 密码='" + textBox2.Text + "'";
            string myconstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.mdb";
            OleDbCommand mycom = null;
            OleDbConnection mycon = null;
            try
            {
                mycon = new OleDbConnection(myconstr);
                mycon.Open();
                mycom = new OleDbCommand(SQL, mycon);
                OleDbDataReader rd = mycom.ExecuteReader();
                if (rd.HasRows)
                {
                    rd.Read();
                    this.Hide();
                    Form1 m = new Form1();
                    m.ShowDialog();
                     this.textBox1.Clear();
                    this.textBox2.Clear();
                    this.textBox1.Focus();
                   
                }
                else
                {
                    MessageBox.Show("没有这个用户或密码不正确,请重新登录!");
                    return;
                }
            }
            catch (OleDbException oe)
            {
                MessageBox.Show(oe.Message, "Error");
            }
            finally
            {
                if (mycon.State == ConnectionState.Open)
                    mycon.Close();
            }
        }

注册源代码:

              string ConStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:DB.mdb";
            OleDbConnection cn = new OleDbConnection(ConStr);
            try
            {
                cn.Open();
                string insert = "insert into 用户表(userid,psw)values";
                insert += "('" + textBox1.Text + "','" + textBox2.Text + "')";
                OleDbCommand cm = new OleDbCommand(insert, cn);
                DataSet mydata = new DataSet();

                OleDbDataAdapter myadapter = new OleDbDataAdapter(cm);
                if (textBox1.Text == "" || textBox2.Text == "")
                {
                    MessageBox.Show("所有项都是必填项,请填完后再单击注册按钮");
                    return;
                }
                else
                myadapter.Fill(mydata, "用户表");
                MessageBox.Show("注册成功!");
            }
            catch (OleDbException te)
            {
                MessageBox.Show(te.Message);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }

        }
    }

模糊查询源代码:

        //按姓名查找
        private void button1_Click(object sender, EventArgs e)
        {
            string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.mdb;";
            OleDbConnection Con = new OleDbConnection(ConStr);
            string str = "select * from 学生表 ";
            str += "where 姓名 like '%" + textBox1.Text.Trim() + "%'";
            OleDbCommand Com = new OleDbCommand(str, Con);
            OleDbDataAdapter myda = new OleDbDataAdapter();
            myda.SelectCommand = Com;
            Con.Open();            
            DataSet myds = new DataSet();
            MessageBox.Show(str);
            myda.Fill(myds, "学生表");
            Con.Close();
            dataGridView1.DataSource = myds;
            dataGridView1.DataMember = "学生表";
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值