简体   繁体   English

C# System.InvalidOperationException 错误

[英]C# System.InvalidOperationException error

I wanted to make a password generator and it should save the password for what you want to use the password then I wanted that the program displays all the datas in my database.我想制作一个密码生成器,它应该为你想要使用的密码保存密码,然后我希望程序显示我数据库中的所有数据。 so, I can look at it.所以,我可以看看。 Also, I am using Console Application(.NET Framwork).When I used the program without displaying the data, it worked.另外,我正在使用控制台应用程序(.NET 框架)。当我使用该程序而不显示数据时,它可以工作。 But, when I put my displaying code, it makes that error.但是,当我放置我的显示代码时,它会出错。

System.InvalidOperationException Connection must be valid and open. System.InvalidOperationException 连接必须有效且打开。

I don't know how to fix that.我不知道如何解决这个问题。 It would be super cool, if you guys can help me and make me more smart:D Here is the program code:如果你们能帮助我,让我变得更聪明,那就太酷了:D 这是程序代码:

string verbindungsdaten = "SERVER = localhost;" +
                          "DATABASE = db;" +
                          "UID = Sarper;" +
                          "PASSWORD = Bmw03082009;";

MySqlConnection verbindung = new MySqlConnection(verbindungsdaten);

Random meinrandom = new Random();
int zahl = meinrandom.Next(100000, 1000000);
Console.WriteLine("Ihr Passwort ist zwischen den Werten: 100000 - 1000000");
Console.Write("Geben sie bitte die Benutzung vom Passwort damit sie es 
wiederverwenden können ein: ");
string benutzung1 = Console.ReadLine();
Console.WriteLine("Ihr Passwort wurde generiert: " + zahl);
Console.WriteLine("Alle Daten im überblick mit Benutzung: ");

string command = "INSERT INTO passwörter VALUES ('" + benutzung1 + "', '" + zahl + "');";

MySqlCommand ausführen = new MySqlCommand(command);
ausführen.Connection = verbindung;

string com = "SELECT * FROM passwörter;";
MySqlCommand com11 = new MySqlCommand(com);

verbindung.Open();
ausführen.ExecuteNonQuery();
MySqlDataReader Reader;
    
Reader = com11.ExecuteReader();
Reader.Read();
Console.WriteLine("Benutzung " + Reader.GetValue(0).ToString());
Console.WriteLine("Passwort " + Reader.GetValue(1).ToString());
verbindung.Close();

It is german again because I am from Germany.That are only things in the WriteLine codes, but I think you can unserstand it.它又是德语,因为我来自德国。这只是 WriteLine 代码中的内容,但我认为您可以理解它。 :D :D

you never assigned a connection to com11您从未为 com11 分配连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM