In the past two days, we have developed a winform software that needs to connect to the database of the server. config to configure the database connection string. The default timeout time is 15 seconds (some people say it is 30 seconds, this is not clear, and there is no time to perform special verification ).
When the software is started, a piece of code is written to verify whether the database connection is normal. According to the default timeout, once the database connection fails, the wait time is too long and you want to change the timeout to 5 seconds. However, when I click the SqlConnection instance, I find that ConnectTimeout is a read-only attribute. I am depressed. When I access Baidu, most people are in the App. config, but I just want to modify the timeout value at startup. In other cases, I want to use the default value. What should I do?
Later, I tried to find another class SqlConnectionStringBuilder, which may also be the help provided by online users. Get the ConfigurationManager App. the connection string information in Config is converted to SqlConnectionStringBuilder, and then the Timeout attribute in the SqlConnectionStringBuilder instance is modified, and then an instance is sqlconnected. The parameters of the constructor are the same as those of the SqlConnectionStringBuilder instance. toString (), finally run the program, the effect is coming out, the waiting time is shorter than before, there is no test time, the visual test should be 5 seconds.
Many experts may know this, But Baidu's answers are not satisfactory. Therefore, record and share the answer with others. In addition, you are welcome to add the experts!