using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
private const string sql = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
static void Main(string[] args)
{
Program p = new Program();
string s3 = "";
bool b = p.show("Admin", "0", ref s3);
if (b)
{
Console.WriteLine(s3);
}
else
{
Console.WriteLine(s3);
}
Console.ReadLine();
}
public bool show(string s1, string s2, ref string s3)
{
SqlConnection connection = new SqlConnection(sql);
try
{
connection.Open();
Console.WriteLine("打开数据库");
string sql1 = @"SELECT COUNT(*)FROM [MySchool].[dbo].[Admin]
where Loginid='" + s1 + "'and Loginpwd='" + s2 + "'";
Console.WriteLine(sql1);
SqlCommand connection1 = new SqlCommand(sql1, connection);
int a = (int)connection1.ExecuteScalar();
if (a > 0)
{
s3 = "登陆成功!";
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("数据库有问题");
}
finally
{
connection.Close();
Console.WriteLine("关闭数据库");
}
s3 = "登陆失败!";
return false;
}
}
}C# 数据库 检查用户信息 验证登陆 十四章
最新推荐文章于 2022-07-26 14:06:55 发布
这是一个使用C#连接SQL Server数据库,检查并验证用户登录信息的示例。程序通过建立数据库连接,执行SQL查询来判断用户名和密码是否匹配,从而实现登录验证功能。

1万+

被折叠的 条评论
为什么被折叠?



