You need to enable JavaScript to run this app. .NET Web API调用SQL Server存储过程插入数据报ConnectionString未初始化错误
优惠活动
大模型
产品
解决方案
定价
更多

.NET Web API调用SQL Server存储过程插入数据报ConnectionString未初始化错误

故障场景

开发.NET Web API 用户注册功能时,通过SQL Server存储过程实现用户数据入库操作,程序抛出 The ConnectionString property has not been initialized 异常。前期已调整多版连接字符串配置,问题始终未解决。
调试定位结果:程序执行到doRegister方法内的db.ExecNonQuery("SSO_Register", parm)代码行时触发异常。

现有配置与实现代码

数据库连接字符串配置

<connectionStrings>
    <add name="_cs" 
         connectionString="Data Source=DESKTOP-KR81RU5\MSSConQLSERVER01;Initial Catalog=HISDB;Integrated Security=True;Connect Timeout=3600" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

dbUtil 工具类实现

private static string _cs = null;

public dbUtil(string cs)
{
    _cs = cs;
}

public void ExecNonQuery(string sp, SqlParameter[] parm)
{
    using (SqlConnection connection = new SqlConnection(_cs))
    {
        using (SqlCommand command = new SqlCommand(sp, connection))
        {
            command.CommandType = CommandType.StoredProcedure;
            command.CommandTimeout = 120;

            connection.Open();

            if (parm != null)
            {
                for (int i = 0; i < parm.Length; i++)
                    command.Parameters.Add(parm[i]);
            }

            command.ExecuteNonQuery();
        }
    }
}

doRegister 注册方法实现

bool _return = false;

try
{
    string tac = " ";

    Test util = new Test();
    tac = Test.getTac();

    SqlParameter[] parm =
                {
                    new SqlParameter("secure_word", objsso.secure_word),
                    new SqlParameter("full_name", objsso.full_name),
                    new SqlParameter("phone_number", objsso.phone_number),
                    new SqlParameter("nationality", objsso.nationality),
                    new SqlParameter("id_number", objsso.id_number),
                    new SqlParameter("dob", objsso.dob),
                    new SqlParameter("email", objsso.email),
                    new SqlParameter("user_id", objsso.email),
                    new SqlParameter("full_name", objsso.full_name),
                    new SqlParameter("provider_id", objsso.provider_id),
                    new SqlParameter("temp_token", objsso.temp_token),
                    new SqlParameter("tac", tac)
            };

    dbUtil db = new dbUtil(_cs);
    db.ExecNonQuery("SSO_Register", parm);
    _return = true;
}
catch (Exception ex)
{
    _return = false;
    throw ex;
}

return _return;

根因定位

该异常触发的核心条件是SqlConnection实例调用Open()方法时,内部存储的连接字符串为null或空字符串,和连接字符串本身的格式配置无关,从现有代码可定位到两处明确逻辑缺陷:

  • 代码未实现配置读取逻辑:现有流程完全没有从应用配置文件中加载已定义连接字符串的代码,调用new dbUtil(_cs)时,传入的参数是当前类中默认值为null的_cs字段,从未和配置中name="_cs"的连接字符串做关联,最终传入SqlConnection的连接字符串为null
  • 字段设计存在缺陷:dbUtil类中的_cs字段被定义为静态变量,构造函数给静态字段赋值的逻辑在Web API高并发场景下,会出现多请求互相覆盖字段值的问题,进一步放大连接字符串为空的偶发概率

修复步骤

  • 补充配置读取逻辑:根据项目框架版本,使用对应API加载配置中的连接字符串值
    • .NET Framework 项目:引入System.Configuration程序集,通过ConfigurationManager.ConnectionStrings["_cs"].ConnectionString读取配置值
    • .NET Core/.NET 5+ 项目:通过注入的IConfiguration实例,调用GetConnectionString("_cs")拿到对应配置值
  • 修正dbUtil类的字段设计:移除_cs字段的static修饰,改为实例私有字段,避免并发下的字段值串改;在构造函数中增加非空校验,传入空连接字符串时直接抛出明确的参数错误,避免到数据库连接阶段抛出模糊异常
  • 修复参数定义冗余问题:现有参数数组中重复定义了两次full_name参数,执行存储过程时会触发SqlParameter重复的异常,需删除重复的参数条目

内容的提问来源于stack exchange,提问作者Taqiuddin Shokordey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 06:45:04