本文介绍了检索连接字符串时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace hospitalanagementsystem
{
    internal class utility
    {
        //get the connection string from app config file
        internal static string Connection()
        {
            //assuming failure in retrieval
            string returnValue = null;
            //look for the name in the connectionstring section
            ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["hospitalanagementsystem.Properties.Settings.constr"];

            //if found, return the connectionstring 
            if(setting!=null)
            {
                returnValue = setting.ConnectionString;
                return returnValue;
            }

        }

    }
}

上面的代码我在函数名称Coneection中出错了/>
错误说并非所有代码路径都返回空值请帮我做什么

谢谢

in the above code i m having an error in the function name Coneection
the error says "not all code paths return a null value" please help me what should i do
thank you

推荐答案


//get the connection string from app config file
internal static string Connection()
{
    //assuming failure in retrieval
    string returnValue = null;
    //look for the name in the connectionstring section
    ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["hospitalanagementsystem.Properties.Settings.constr"];

    //if found, return the connectionstring 
    if(setting!=null)
    {
        returnValue = setting.ConnectionString;
    }
   return returnValue; // moved this line to end, even in settings null you have return value now
}


这篇关于检索连接字符串时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:48