Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

c# - Connection String Is not Working Object instance reference is null

//'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.'///

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...