Asp.net System.NullReferenceException 创建新的 MysqlConnection 时

Asp.net System.NullReferenceException when creating new MysqlConnection

`我是 asp.net 的新手,当我尝试连接到 MySQL 数据库时出现此错误。 error message

这是我在 web.config 文件中的代码

<configuration>

  <appSettings>
    
      <add key="HostPrefix" value="/root" />
      <add key="ServerIp" value="localhost" />
      <add key="MysqlConnectionstring" value="server=localhost;Database=world;port=3306;user=root;password=1234;SslMode=none" />
      <add key="UserImagePath" value="world\city\" />
      
  </appSettings>

这是我的数据库连接代码

using Common;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace Repositories
{
    public class StudentRepository
       
    {
         private string ConnectionstringMaster = WebConfigurationManager.AppSettings["MysqlConnectionString"];

  private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();        
       
public studentsModel UserRegistration(string Id )
        { studentsModel stud1 = new studentsModel();     

try
             {
string Query = "select ID,Name,CountryCode,District from world.city where ID='" + Id + "'";
using (var connection = new MySqlConnection(ConnectionstringMaster))
                 
                 {
                      using (var command= new MySqlCommand(Query,connection))
                      {
                          connection.Open();
                          using (var sdr=command.ExecuteReader())
                          {
                              while (sdr.Read())
                              {
                                  
                                  stud1.FirstName = sdr["Name"].ToString();

                              }

                          }
                      }
                  }
                  return stud1;
                
                
            }
            catch (Exception e)
             {
                Logger.Info("inside Exception Repo Userregistration  "+e.ToString());
              
                return stud1;
             } 
            

           
        }
    }
}

我在这里尝试从数据库中获取数据并将这些值分配给 studentmodel stud1 。但是当我尝试在本地主机中与我的数据库创建新的 MySQL 连接时 它给出了这样的异常。

ispasswordExpired :connection.ispasswordExpired 引发了类型为 'System.nullReferenceException'

的异常

ServerThread :connection.ServerThread 引发了 'System.nullReferenceException'

类型的异常

ServerVersion : connection.ServerVersion 引发了 'System.nullReferenceException'

类型的异常

您需要将 connectionStrings 添加到您的 Web.Config。

Web.config:

<configuration>
  <connectionStrings>
    <add name="MysqlConnectionstring" connectionString="Server=myServerName;Port=3306;Database=myDatabaseName;Uid=testUser;Pwd=myPassword;" />      
  </connectionStrings>
                     ...
</configuration>

您首先需要在 web.config 文件中添加 connectionStrings 部分,然后将其从 appSettings 部分中删除,如下所示:

<configuration>

  <connectionStrings>
    <add name="MysqlConnectionstring" connectionString="server=localhost;Database=world;port=3306;user=root;password=1234;SslMode=none" />
  </connectionStrings>
  
  <appSettings>  
      <add key="HostPrefix" value="/root" />
      <add key="ServerIp" value="localhost" />
      <add key="" value="" />
      <add key="UserImagePath" value="world\city\" />   
  </appSettings>
 
</configuration>

然后您可以通过访问代码中的 ConnectionString 属性 来访问连接字符串值,如下所示:

private string ConnectionstringMaster = ConfigurationManager.ConnectionStrings["MysqlConnectionstring"].ConnectionString;