如何将c#控制台连接到数据库?

如何将c#控制台连接到数据库?,c#,login,C#,Login,我正在制作一个c#控制台应用程序,并想添加一个登录系统 我已经有了这个代码: Start: Console.Clear(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Enter Username."); string strUsername = Console.ReadLine(); string strTUsername =

我正在制作一个c#控制台应用程序,并想添加一个登录系统

我已经有了这个代码:

Start:
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.WriteLine("Enter Username.");
        string strUsername = Console.ReadLine();
        string strTUsername = "Test";

        if (strUsername == strTUsername)
        {
            Console.Clear();
            Console.WriteLine("Enter Password");
            Console.ForegroundColor = ConsoleColor.Gray;
            string strPassword = Console.ReadLine();
            string strTPassword = "Test";
            if ( strPassword == strTPassword)
            {
                Console.Write("Logging in.");
                Thread.Sleep(1000);
            }

            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("The password you entered is wrong.");
                Thread.Sleep(2000);
                goto Start;
            }
        }

        else
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("The username you entered is wrong.");
            Thread.Sleep(2000);
            goto Start;
        }
我想让它允许多个用户名和密码,你可以输入,将工作

到目前为止,它只接受用户名和密码“Test”,但我想将它链接到另一个填充用户名和密码的文件,我可以使用它,而不仅仅是“Test”


你能给我的任何帮助都是有用的,谢谢

有两种方法:

1: Database to store username and password
2: Save the username and password in file in a uniform format(like comma,tab separated)
1:数据库

->Select a database to use
->create a table with columns such as username and password
->connect to database from your app
->get username from console and compare it with the rows of database and check if the password given is correct.
2:文件

->Save a file with username and password with a certain format(comma,space or tab separated)
->Import those from the file to a Dictionay<users>.
->compare the entered password and user name with the dictionary items.
->使用用户名和密码以特定格式(逗号、空格或制表符分隔)保存文件
->将这些从文件导入词典。
->将输入的密码和用户名与字典项进行比较。
您可以使用加密使文件或数据库更加安全

static void Main(string[] args)
        {
            List<User> usersList = new List<User>();
            string[] lines = System.IO.File.ReadAllLines("users.txt");
            foreach ( var line in lines)
            {
                User user = new User();
                user.user = line.Split(' ')[0];
                user.password = line.Split(' ')[1];
                usersList.Add(user);
            }
            foreach (var item in usersList)
            {
                Console.WriteLine(item.user);
                Console.WriteLine(item.password);
            }
            Console.ReadLine();
        }



}
public class User
{
   public string user { get; set; }
   public  string password { get; set; }
}
static void Main(字符串[]args)
{
List usersList=new List();
string[]lines=System.IO.File.ReadAllLines(“users.txt”);
foreach(行中的var行)
{
用户=新用户();
user.user=line.Split(“”)[0];
user.password=line.Split(“”)[1];
usersList.Add(用户);
}
foreach(usersList中的var项)
{
Console.WriteLine(item.user);
Console.WriteLine(项目.密码);
}
Console.ReadLine();
}
}
公共类用户
{
公共字符串用户{get;set;}
公共字符串密码{get;set;}
}

在本文中,我添加了一个简单的代码来读取空格分隔的密码文件,并根据您的要求使用它。为了获得更安全的方式,可以对文件进行加密。谢谢你

不管怎样,如果我能为不同的个人资料输入多个用户名和密码,那对我来说就行了。你使用
goto
会被钉死的。我还能用什么?lol@Drips使用while或for循环,并在用户提供正确密码时退出循环。:)提示一下,我不会在任何外部数据库/文件中以明文形式存储密码。相反,做一些散列并测试用户输入的密码。同样,您的代码中也没有任何明文密码,您可以很容易地看到.net应用程序的源代码。我可以使用什么来创建数据库?我如何将控制台连接到数据库?您想使用哪个数据库?@soumyasambitKunda任何我可以使用的可以工作的东西,我的选项是什么?如果您想以安全的方式将密码保存在本地系统中,我建议使用密码保险库:)@Drips如果有意义,我可以发布链接和代码。