连接字符串不起作用对象实例引用为空

时间:2018-07-29 14:07:27

标签: c# asp.net sql-server connection-string sqlconnection

//“对象引用未设置为对象的实例。” 请不要将其标记为多余的问题。 我尝试了几乎所有方法来制作连接字符串

第一个是:

string connectionString = ConfigurationManager.ConnectionStrings["ClinicalConnectionString"].ConnectionString;


第二个:

   string connectionstringgg = Properties.Settings.Default.ClinicalConnectionString;


第三种方法是:

 ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings["ClinicalConnectionString"];

//我建立的连接的名称

SqlConnection connection =新的SqlConnection(connectionString);

在应用配置中:

<connectionStrings>
    <add name="ClinicalDAO.Properties.Settings.ClinicalConnectionString"
        connectionString="Data Source=DESKTOP-I07DSQC;Initial Catalog=db_clinics;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

在网络配置中

  <connectionStrings>
    <add name="ClinicalDAO.Properties.Settings.ClinicalConnectionString"
        connectionString="Data Source=DESKTOP-I07DSQC;Initial Catalog=db_clinics;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

任何帮助,仍然会给我同样的错误://///'对象引用未设置为对象的实例。'///

1 个答案:

答案 0 :(得分:1)

有许多方法可以在C#应用程序中连接到SQL Server数据库。

  1. 不推荐的第一种方法是硬编码:

    public void CreateMySqlConnection()
    {
        MySqlConnectionStringBuilder myCSB = new MySqlConnectionStringBuilder();
        myCSB.Port = 3307;
        myCSB.Host = "localhost";
        myCSB.UserId = "root";
        myCSB.Password = "mypassword";
        myCSB.Direct = true;
        myCSB.Compress = true;
        myCSB.Database = "demobase";
        myCSB.MaxPoolSize = 150;
        myCSB.ConnectionTimeout = 30;
        MySqlConnection myConnection = new MySqlConnection(myCSB.ConnectionString);
    }
    

来自:https://www.devart.com/dotconnect/connection-strings.html?gclid=CjwKCAjwy_XaBRAWEiwApfjKHt-Yn6Ja43anKj0cvAzDHL5eNDHKvaxwnq5IEsVyHY-rR3GECsa6shoCZH8QAvD_BwE

  1. 第二种方式是在作者评论中回答@sellotape已经解决的问题,是将连接字符串放在您的web.config上:

    <add name="MovieDB"
         connectionString="Data Source=LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf"     
         providerName="System.Data.SqlClient"/>
    

阅读:

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["MovieDB"];
            if (connString != null)
                Console.WriteLine("MovieDB connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No MovieDB connection string");
        }

您的web.config标记“名称”上的名称

    <add name="MovieDB".....

必须与您的C#代码相同:

    connString = rootWebConfig.ConnectionStrings.ConnectionStrings["MovieDB"]

您无需像以前一样指定大名:“ ClinicalDAO.Properties.Settings.ClinicalConnectionString”

使其更小,更简单。

来自:https://msdn.microsoft.com/en-us/library/ms178411.aspx

别忘了在web.config中保护您的连接字符串。 请阅读这个: https://msdn.microsoft.com/en-us/library/ms178372.aspx