Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当读卡器关闭时尝试读取无效。C#Mysql_C#_Mysql - Fatal编程技术网

当读卡器关闭时尝试读取无效。C#Mysql

当读卡器关闭时尝试读取无效。C#Mysql,c#,mysql,C#,Mysql,此脚本位于我的visual basic应用程序的注册页上。 只有在我关闭应用程序时,脚本才会将数据保存在数据库中,不会出现任何错误。当读卡器关闭时,脚本会给我一个错误,即尝试读取时无效。这只发生在这一页上 if (PasswordRegister.Text == RepeatPasswordRegister.Text) { if (PasswordRegister.Text == "") { Me

此脚本位于我的visual basic应用程序的注册页上。 只有在我关闭应用程序时,脚本才会将数据保存在数据库中,不会出现任何错误。当读卡器关闭时,脚本会给我一个错误,即尝试读取时无效。这只发生在这一页上

 if (PasswordRegister.Text == RepeatPasswordRegister.Text)
        {

            if (PasswordRegister.Text == "")
            {
                MessageBox.Show("Enter a vailed password");
            }
            else if (UsernameRegister.Text == "")
            {
                MessageBox.Show("Enter a vailed Username");
            }
            else {

            string myConnection = "datasource=127.0.0.1;port=3306;username=root;password=Root;";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand Login = new MySqlCommand("select * from database.users where Username='" + this.UsernameRegister.Text + "' ;", myConn);

            MySqlDataReader RegisterReader;
            myConn.Open();
            RegisterReader = Login.ExecuteReader();
            int count = 0;
            while (RegisterReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                MessageBox.Show("Username is taken");
            }
            else if (count > 1)
            {
                MessageBox.Show("ERROR Contact support");
            }
            else {


                    string constring = "datasource=127.0.0.1;port=3306;username=root;password=Root;";
                    string Query = "insert into database.users (Username, Password) values ('" + this.UsernameRegister.Text + "','" + this.PasswordRegister.Text + "') ;";
                    MySqlConnection conDataBase = new MySqlConnection(constring);
                    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
                    MySqlDataReader myReader;
                    try
                    {
                        conDataBase.Open();
                        myReader = cmdDataBase.ExecuteReader();
                        MessageBox.Show("Register Succes");
                        conDataBase.Close();
                        conDataBase.Dispose();
                        this.Hide();
                        Form1 home = new Form1();
                        home.ShowDialog();
                        while (myReader.Read())
                        {

                        }
                    }


                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }
            myConn.Close();
        }
        }
        else {
            MessageBox.Show("Check password");
        };

如果仔细查看代码,您会注意到在尝试从读卡器读取之前已关闭连接:

conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
conDataBase.Close(); *** Connection closed here
conDataBase.Dispose(); *** And disposed disposed, for good measure
...
while (myReader.Read())  *** Now you are trying to Read!
{

}
你需要做的是保持连接打开直到你读完,例如

using (var myConn = new MySqlConnection(myConnection))
using (var Login = new MySqlCommand("select * from database.users where Username= @myUserName", 
       myConn);
{
  cmdDataBase.AddWithValue(new MySqlParameter("@myUserName", this.UsernameRegister.Text));
  conDataBase.Open();
  using (var myReader = cmdDataBase.ExecuteReader())
  {
      while (myReader.Read())
      {
           // Do something with myReader[] fields here
      }
  } // Reader disposed
} // Command Disposed, Connection Closed + Disposed

我还希望
使用
语法来管理连接、命令和读卡器等一次性设备,同时参数化您的查询和命令-这具有安全性,可能还有性能优势。

在使用读卡器之前关闭连接。