Connection String Is not Working Object instance reference is null

こ雲淡風輕ζ 提交于 2019-12-26 12:39:07

问题


//'Object reference not set to an instance of an object.' Please Do not mark it as redundant question . I have tried almost all the methods to make a connection string

first one is by :

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


the second one :

   string connectionstringgg = Properties.Settings.Default.ClinicalConnectionString;


third method is by :

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

//the name of the connection i made

SqlConnection connection = new SqlConnection(connectionString);

In the app config:

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

In the web config

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

ANY HELP PLEASE it still give me the same error : /////'Object reference not set to an instance of an object.'///


回答1:


There are many ways to connect to your SQL Server database within a C# app.

  1. The First way, that is not recommended, is hard coding:

    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);
    }
    

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

  1. The second way, anwsering the question being already anwsered by @sellotape at the author comments, is puting the connection string at you 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"/>
    

to read it:

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");
        }

The name at your web.config tag 'name'

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

has to be the same one from your c# code:

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

You don´t need to specify a large name as you did: "ClinicalDAO.Properties.Settings.ClinicalConnectionString"

Make it smaller and simple.

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

Don´t forget to secure your connection string at your web.config. Please, read this: https://msdn.microsoft.com/en-us/library/ms178372.aspx



来源:https://stackoverflow.com/questions/51581130/connection-string-is-not-working-object-instance-reference-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!