C#连接MYSQLSQEVE数据库示例代码

来源:互联网 发布:财务金融知乎 编辑:程序博客网 时间:2024/04/30 23:21

功能简介:

    1、实现用户登录验证

     2、实现身份验证

     3、实现数据库数据存取

代码如下:

winform form窗体内的代码:

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


namespace Course_Windows
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnlogin_Click(object sender, EventArgs e)
        {
          if (txtUserName.Text  == "")
         {
             MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             txtUserName.Focus();
         }
          else if (txtPasword.Text == "")
          {
              MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              txtPasword.Focus();
          }
          else 
          {
              if(radAdmin.Checked)
              {
                  DBhelp.userrole=1;
              }
              else
              {
                 DBhelp.userrole=0;
              }
              string sql = "select * from tb_user where uname='" + txtUserName.Text.Trim() + "' and upassword='" + txtPasword.Text  + "' and  urole=" + DBhelp.userrole + "";
              SqlDataReader sqlread;
              sqlread = DBhelp .read(sql);
              if(sqlread.HasRows)
              {
                  MessageBox.Show("登陆成功");
              }
              else
              {
                  MessageBox.Show("请输入正确信息","警告");
              }
          }
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            //DialogResult dia;
            //dia = MessageBox.Show(this, "是否退出", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //if (DialogResult.OK == dia)
            //{
            //    e.Cancel = false;
            //}
            //else
            //{
            //    e.Cancel = true;
            //}
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            radAdmin.Checked = true;


        }
    }
}


创建DBhelp类,代码为:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;




using System.Configuration;
using System.Data;








namespace Course_Windows
{
    class DBhelp
    {
        public static string strconn = @" Data Source=*******;Initial Catalog=#######;Integrated Security=True;Pooling=False";//*****代表数据源,####代表数据库名


        public static int userrole;//用户类型




        public static SqlDataReader read(string sql)
        {
            SqlConnection sqlconn = new SqlConnection(strconn);


            try
            {
                sqlconn.Open();  
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            SqlCommand comm = new SqlCommand(sql, sqlconn);
            SqlDataReader sqlread = comm.ExecuteReader(); 
            return sqlread;
        }


        public static DataSet Search(string sql)
        {
            SqlConnection sqlconn = new SqlConnection(sql);
            try
            {
                sqlconn.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
          
            SqlDataAdapter da = new SqlDataAdapter(sql,sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
            
        }


    }
}

0 0
原创粉丝点击