C#窗体实现用户登录,文件下载与上传,校验文件MD5码与管理员的用户管理功能----源码

本文主要是为了存放源码,想要了解具体功能实现请跳转另一篇博文:具体过程(点我)

App.config

    <connectionStrings>
		<add name ="conn" connectionString="Server =.; uid = sa; pwd = 123; DataBase = mimaxue"/>
    </connectionStrings>

login.cs ------登录模块

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 密码学大作业
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private void 登录_Click(object sender, EventArgs e)
        {
            string username = this.username.Text;
            string password = this.password.Text;
            string yzm = this.yanzhengma.Text;
            if (yanzhengma.Text.ToLower() != label4.Text.ToLower())   //验证码输入错误
            {
                MessageBox.Show("您输入的验证码有误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                label4_Click(sender, e);   //刷新一遍验证码
                yanzhengma.Text = "";    //清空验证码输入框
                return;
            }
            //创建MD5加密对象
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            //把加密的数据转化为字节数组
            byte[] bytes = Encoding.UTF8.GetBytes(password);
            //对字节数组进行加密
            bytes = md5.ComputeHash(bytes);
            string pwd = BitConverter.ToString(bytes).Replace("-", "");
            //第一 读取APP.config文件中的连接字符串
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            //第二 创建连接对象并指定连接
            SqlConnection conn = new SqlConnection(conStr);
            //第三 打开连接
            conn.Open();
            第四 实现数据库相关操作
            //判断是否连接成功
            //if (conn.State == ConnectionState.Open)
            //{
            //    MessageBox.Show("连接成功!");
            //}
            //else
            //{
            //    MessageBox.Show("连接失败!");
            //}
            //通过其他的ADO对象,实现数据库的相关操作
            //查找管理员
            //1)构造SQL查询字符串(含参)
            string sql = string.Format("select * from users where username = @username and password = @pwd");
            //2)创建SqlParameter对象,并设置参数值
            SqlParameter param1 = new SqlParameter("username", username);
            SqlParameter param2 = new SqlParameter("pwd", pwd);
            //3)创建命令对象SqlCommand并添加参数
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Parameters.Add(param2);
            SqlDataReader reader = cmd.ExecuteReader();
            if (!reader.HasRows)
            {
                MessageBox.Show("用户名或密码错误!请重试", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.username.Text = "";
                this.password.Text = "";
                this.yanzhengma.Text = "";
                label4.Text = CreateRandomCode(4);   //生成一个4位的验证码,并显示在标签lable上。
                return;
            }
            while (reader.Read())
            {
                string shenfen = reader["shenfen"].ToString();
                string sname = reader["username"].ToString();
                string str = string.Format("你好{0}:{1}! 欢迎使用本系统 ", shenfen, sname);
                MessageBox.Show(str);
            }
            reader.Close();
            //第五 关闭连接
            conn.Close();
            //跳转到主界面
            this.Hide();
            main main = new main();
            main.Show();

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt用户名_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            regist regist = new regist();
            regist.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("感谢您的使用,再见!");
            System.Environment.Exit(0);
        }

        private void login_Load(object sender, EventArgs e)
        {
            label4.Text = CreateRandomCode(4);   //生成一个4位的验证码,并显示在标签lable上。
        }
        public static string CreateRandomCode(int length)  //生成由数字和大小写字母组成的验证码
        {
            string list = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
            //list中存放着验证码的元素
            Random random = new Random();
            string code = "";   //验证码
            for (int i = 0; i < length; i++)   //循环6次得到一个伪随机的六位数验证码
            {
                code += list[random.Next(0, list.Length - 1)];
            }
            return code;
        }

        private void label4_Click(object sender, EventArgs e)
        {
            label4.Text = CreateRandomCode(4);   //生成一个4位的验证码,并显示在标签lable上。
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string username = this.username.Text;
            string password = this.password.Text;
            string yzm = this.yanzhengma.Text;
            if (yanzhengma.Text.ToLower() != label4.Text.ToLower())   //验证码输入错误
            {
                MessageBox.Show("您输入的验证码有误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                label4_Click(sender, e);   //刷新一遍验证码
                yanzhengma.Text = "";    //清空验证码输入框
                return;
            }
            //创建MD5加密对象
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            //把加密的数据转化为字节数组
            byte[] bytes = Encoding.UTF8.GetBytes(password);
            //对字节数组进行加密
            bytes = md5.ComputeHash(bytes);
            string pwd = BitConverter.ToString(bytes).Replace("-", "");
            //第一 读取APP.config文件中的连接字符串
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            //第二 创建连接对象并指定连接
            SqlConnection conn = new SqlConnection(conStr);
            //第三 打开连接
            conn.Open();
            第四 实现数据库相关操作
            //判断是否连接成功 
            //if (conn.State == ConnectionState.Open)
            //{
            //    MessageBox.Show("连接成功!");
            //}
            //else
            //{
            //    MessageBox.Show("连接失败!");
            //}
            //通过其他的ADO对象,实现数据库的相关操作
            //查找管理员
            string sql = string.Format("select * from users where username = @username and password = @pwd and shenfen = '管理员'");
            //2)创建SqlParameter对象,并设置参数值
            SqlParameter param1 = new SqlParameter("username", username);
            SqlParameter param2 = new SqlParameter("pwd", pwd);
            //3)创建命令对象SqlCommand并添加参数
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Parameters.Add(param2);
            SqlDataReader reader = cmd.ExecuteReader();
            if (!reader.HasRows)
            {
                MessageBox.Show("您不是管理员,请用用户身份登录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.username.Text = "";
                this.password.Text = "";
                return;
            }
            while (reader.Read())
            {
                string shenfen = reader["shenfen"].ToString();
                string sname = reader["username"].ToString();
                string str = string.Format("你好{0}:{1}! 欢迎使用本系统 ", shenfen, sname);
                MessageBox.Show(str);
            }
            reader.Close();
            //第五 关闭连接
            conn.Close();
            this.Hide();
            zxq zxq = new zxq();
            zxq.Show();
        }
    }
}

regist.cs ------注册模块

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 密码学大作业
{
    public partial class regist : Form
    {
        public regist()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = this.tb1.Text;
            string pwd1 = tb2.Text;
            string pwd2 = tb3.Text;
            if (pwd1.Length != 8)
            {
                MessageBox.Show("请设置一个8位密码");
                if (pwd1.Length != pwd2.Length)
                {
                    MessageBox.Show("两次密码不相同,请核对!");
                }
                return;
            }
            if (pwd1 == pwd2)
            {
                //创建MD5加密对象
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                //把加密的数据转化为字节数组
                byte[] bytes = Encoding.UTF8.GetBytes(pwd1);
                //对字节数组进行加密
                bytes = md5.ComputeHash(bytes);
                //第一 读取APP.config文件中的连接字符串
                string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
                //第二 创建连接对象并指定连接
                SqlConnection conn = new SqlConnection(conStr);
                //第三 打开连接
                conn.Open();
                //第四 创建命令对象SqlCommand
                //SqlCommand cmd = new SqlCommand(sql, conn);
                //第五 定义操作数据库的SQL语句,并使用字符串变量接受
                方法一 字符串连接(好恶心)
                //string sql = "insert into users(username,password) values('"+ username +"','"+ pwd1 +"')";
                方法二 string.Format()
                //string sql = string.Format("insert into users(username, password) values('{0}', '{1}')",username,pwd1);
                //方法三 使用SqlParameter对象对参数进行封装:保证安全,防止非法攻击
                //1)定义参数化SQL语法:把操作的数据,定义相关的参数来表示参数前面必须使用@
                string sql = string.Format("insert into users(username, password) values(@username, @pwd)");
                //2)创建SqlParameter对象,并设置参数值
                SqlParameter param1 = new SqlParameter("username", username);
                SqlParameter param2 = new SqlParameter("pwd", BitConverter.ToString(bytes).Replace("-", ""));
                //3)创建命令对象SqlCommand并添加参数
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(param1);
                cmd.Parameters.Add(param2);
                //第六 设置连接对象,
                cmd.Connection = conn;
                //第七 设置命令对象要执行的SQL语句
                cmd.CommandText = sql;
                //第八 执行SQL语句
                try
                {
                    int r = cmd.ExecuteNonQuery();
                    //第九 判断是否成功
                    if (r == 1)
                    {
                        MessageBox.Show("注册成功!即将返回登陆界面");
                        this.Close();
                        login login = new login();
                        login.Show();
                    }
                }
                catch
                {
                    MessageBox.Show("已存在该用户!");
                    tb1.Text = "";
                    tb2.Text = "";
                    tb3.Text = "";
                    return;
                }
            }
            else
            {
                MessageBox.Show("您输入的两次密码不一样,请核对后重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            login login = new login();
            login.Show();
        }

        private void regist_Load(object sender, EventArgs e)
        {

        }
    }
}

main.cs ------用户功能模块

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 密码学大作业
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
        }

        private void main_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        //选择文件

        private void btnOpenFile_Click(object sender, EventArgs e)

        {
            //利用OpenFileDialog对象,选中某个文件
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                //成功打开后
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    //获取文件名
                    String fileName = dialog.FileName;
                    //先将文本框清空
                    this.txtMD5.Text = "";
                    //利用getMD5Hash()方法生成安全码并显示
                    this.txtMD5.Text = getMD5Hash(fileName);
                }
            }

        }

        //计算文件的MD5码

        private string getMD5Hash(string pathName)

        {

            string strResult = "";

            string strHashData = "";



            byte[] arrbytHashValue;

            System.IO.FileStream oFileStream = null;



            System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =

                       new System.Security.Cryptography.MD5CryptoServiceProvider();



            try

            {

                oFileStream = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);

                arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);//计算指定Stream 对象的哈希值

                oFileStream.Close();

                //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”

                strHashData = System.BitConverter.ToString(arrbytHashValue);

                //替换-

                strHashData = strHashData.Replace("-", "");

                strResult = strHashData;

            }

            catch (System.Exception ex)

            {

                MessageBox.Show(ex.Message);

            }



            return strResult;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            login login = new login();
            login.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("感谢您的使用,再见!");
            System.Environment.Exit(0);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string src_path = dialog.FileName.ToString();
                    string filename = Path.GetFileName(src_path);
                    string dst = @"C:\Users\zxq\Desktop\上传";
                    string path = Path.Combine(dst, filename);
                    try
                    {
                        File.Copy(src_path, path);
                        MessageBox.Show(filename + "上传成功!");
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("已存在该文件,请重命名后上传!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = textBox2.Text;
            //OpenFileDialog dialog = new OpenFileDialog();
            //if (!Directory.Exists(@"C:\Users\zxq\Desktop\上传"))          //如果默认路径文件夹不存在则新建默认路径文件夹
            //{
            //    Directory.CreateDirectory(@"C:\Users\zxq\Desktop\上传");
            //}
            设置默认打开路径(绝对路径)
            //dialog.InitialDirectory = @"C:\Users\zxq\Desktop\上传";
            //if (dialog.ShowDialog() == DialogResult.OK)
            //{
            //    String fileName = dialog.FileName;
            //    this.textBox1.Text = "";
            //    this.textBox1.Text = getMD5Hash(fileName);
            //}
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string src_path = dialog.FileName.ToString();
                    string filename = Path.GetFileName(src_path);
                    string dst = @"C:\Users\zxq\Desktop\下载";
                    string path = Path.Combine(dst, filename);
                    try
                    {
                        File.Copy(src_path, path);
                        MessageBox.Show(filename + "下载成功!");
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("已存在该文件,请勿重复下载!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                String fileName = dialog.FileName;
                this.textBox2.Text = "";
                this.textBox2.Text = "请仔细核对下方安全码确保文件没有被篡改";
                this.textBox1.Text = "";
                this.textBox1.Text = getMD5Hash(fileName);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //string time1 = DateTime.Now.ToShortDateString().ToString();   //2020 - 6 - 27
            //string time2 = DateTime.Now.ToLongTimeString().ToString();    // 18:02:12
            this.label4.Text = DateTime.Now.ToShortDateString().ToString();
            this.label5.Text = DateTime.Now.ToLongTimeString().ToString();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            string username = this.textBox6.Text;
            string pwd1 = this.textBox3.Text;
            string pwd2 = this.textBox4.Text;
            string pwd3 = this.textBox5.Text;
            //新密码加密
            MD5CryptoServiceProvider md52 = new MD5CryptoServiceProvider();
            byte[] bytes2 = Encoding.UTF8.GetBytes(pwd2);
            bytes2 = md52.ComputeHash(bytes2);
            string pwd22 = BitConverter.ToString(bytes2).Replace("-", "");
            //旧密码
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF8.GetBytes(pwd1);
            bytes = md5.ComputeHash(bytes);
            string pwd = BitConverter.ToString(bytes).Replace("-", "");
            //连接数据库
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            string sql = string.Format("select * from users where username = @username and password = @pwd");
            //2)创建SqlParameter对象,并设置参数值
            SqlParameter param1 = new SqlParameter("username", username);
            SqlParameter param2 = new SqlParameter("pwd", pwd);
            //3)创建命令对象SqlCommand并添加参数
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Parameters.Add(param2);
            SqlDataReader reader = cmd.ExecuteReader();
            if (!reader.HasRows)
            {
                MessageBox.Show("用户名或就密码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.textBox3.Text = "";
                this.textBox6.Text = "";
                return;
            }
            if (pwd2 == pwd3)
            {
                if (pwd2.Length != 8)
                {
                    MessageBox.Show("请设置一个8位密码");
                    textBox4.Text = "";
                    textBox5.Text = "";
                    return;
                }
                string conStr2 = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
                SqlConnection conn2 = new SqlConnection(conStr2);
                conn2.Open();
                string sql2 = string.Format("update users set password = '{0}' where username = '{1}'", pwd22, username);
                try
                {
                    SqlCommand cmd2 = new SqlCommand(sql2, conn2);
                    int r = cmd2.ExecuteNonQuery();
                    if (r == 1)
                    {
                        MessageBox.Show("修改成功,请重新登录!");
                        this.Close();
                        login login = new login();
                        login.Show();
                    }
                    else
                    {
                        MessageBox.Show("修改失败,请重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        textBox4.Text = "";
                        textBox5.Text = "";
                        return;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("操作失败,请重试!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("两次新密码不同,请重新输入!");
                textBox4.Text = "";
                textBox5.Text = "";
                return;
            }
        }
    }
}

zxq.cs ------管理员模块

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;

namespace 密码学大作业
{
    public partial class zxq : Form
    {
        public zxq()
        {
            InitializeComponent();
        }

        private void zxq_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("感谢您的使用,再见!");
            System.Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            login login = new login();
            login.Show();
        }

        private void button6_Click(object sender, EventArgs e)              //文件删除
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (!Directory.Exists(@"C:\Users\zxq\Desktop\上传"))          //如果默认路径文件夹不存在则新建默认路径文件夹
            {
                Directory.CreateDirectory(@"C:\Users\zxq\Desktop\上传");
            }
            //设置默认打开路径(绝对路径)
            dialog.InitialDirectory = @"C:\Users\zxq\Desktop\上传";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string src_path = dialog.FileName.ToString();
                string filename = Path.GetFileName(src_path);
                FileInfo file = new FileInfo(src_path);
                try
                {
                    file.Delete();
                    MessageBox.Show(filename + "删除成功!");
                }
                catch (Exception)
                {
                    MessageBox.Show("删除失败,请重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label6.Text = DateTime.Now.ToLocalTime().ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            string sql = string.Format("update users set shenfen = '管理员' where username = @username");
            SqlParameter param1 = new SqlParameter("username", username);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Connection = conn;
            cmd.CommandText = sql;
            try
            {
                int r = cmd.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("提权成功!");
                }
                else
                {
                    MessageBox.Show("查无此人,请核对名称重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败,请重试!");
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            string sql = string.Format("update users set shenfen = '普通用户' where username = @username");
            SqlParameter param1 = new SqlParameter("username", username);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Connection = conn;
            cmd.CommandText = sql;
            try
            {
                int r = cmd.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("降权成功!");
                }
                else
                {
                    MessageBox.Show("查无此人,请核对名称重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败,请重试!");
            }
        }

        private void 删除_Click(object sender, EventArgs e)
        {
            string username = this.textBox2.Text;
            string conStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            string sql = string.Format("delete from users where username = @username");
            SqlParameter param1 = new SqlParameter("username", username);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(param1);
            cmd.Connection = conn;
            cmd.CommandText = sql;
            try
            {
                int r = cmd.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("删除成功!");
                }
                else
                {
                    MessageBox.Show("查无此人,请核对名称重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败,请重试!");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString()))
            {

                SqlDataAdapter sda = new SqlDataAdapter("Select username,shenfen From users", conn);
                DataSet Ds = new DataSet();
                sda.Fill(Ds, "T_Class");

                //使用DataSet绑定时,必须同时指明DateMember 
                this.dataGridView1.DataSource = Ds;
                this.dataGridView1.DataMember = "T_Class";

                //也可以直接用DataTable来绑定 
                this.dataGridView1.Visible = true;
                this.dataGridView1.DataSource = Ds.Tables["T_Class"];
            }
        }
    }
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值