C#编写与数据库交互的登陆系统

                                   C#编写与数据库交互的登陆系统

最近在做WebGis的项目,我负责后端对数据库的操作,比较笨拙,以登陆系统为例写了一些代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Logdemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string ConStr = "Data Source=********;Initial Catalog=*****;Integrated Security=True";
            try
            {
              
                SqlConnection conn = new SqlConnection(ConStr);
                conn.Open();
                if(conn.State==System.Data.ConnectionState.Open)
                {
                    Console.WriteLine("OK");
                    Console.WriteLine("请输入用户名");
                    string UserName = Console.ReadLine();
                    Console.WriteLine("请输入密码");
                    string pwd = Console.ReadLine();
                    int n = pwd.Length;
                    
                        string sql = "select * from  登陆测试 where 用户名='" + UserName + "'";
                         using (SqlCommand cmd = new SqlCommand(sql, conn))

                        {
                        
                           //cmd.CommandText = "select * from  登陆测试 where 用户名=‘" + UserName + "’";
                            using (SqlDataReader reader = cmd.ExecuteReader())
                           {
                            //Console.WriteLine("1");

                           cmd.Connection = conn;
                            if (reader.Read())
                            {
                                string rpwd = reader.GetString(reader.GetOrdinal("密码"));
                            
                                rpwd = rpwd.Substring(0, rpwd.Length - 10+n);
                      
                                int result = string.Compare(rpwd, pwd);
                            
                                if (result==0)
                                    Console.WriteLine("登陆成功!");
                                else Console.WriteLine("登陆失败");


                            }
                            else Console.WriteLine("用户名不存在!");
                           }
                        }
                    }
                }
               
            
            catch(Exception e)
            {
                //Console.WriteLine("Sorry");
            }
            finally
            {

            }
           
            Console.Read();

        }
    }
}

知识点有:
1、数据库的连接
2、数据库的存储格式
3、对不同类型变量的处理和比较

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值