代码之家  ›  专栏  ›  技术社区  ›  smbbb

'成功地与服务器建立了连接,但在登录过程中发生了错误

  •  0
  • smbbb  · 技术社区  · 3 年前

    我正试图将数据库表中的状态列表读取到我正在编写的数据输入表单中的@html.dropdownlistfor中。 我正在使用 SqlClient 在我的控制器中实现这一点。以下是控制器和视图页面的代码。

            [HttpGet]
            public ActionResult DataEntry() 
            {
                var query = "SELECT State, Description FROM dbo.CountryState WHERE Country = 'USA'";
     //I declared the connection string in the beginning of the controller as below
    //string cnn = "Server=mydb;Database=db_registry_data;Trusted_Connection=True;";
    
                List<DataForm> stateList = new List<DataForm>();
                using (SqlConnection con = new SqlConnection(cnn))
                {
                    con.Open();
    
                    SqlCommand cmd = new SqlCommand(query, con);
    
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            DataForm dataForm = new DataForm();
                            dataForm.Description = Convert.ToString(reader["Description"]); // description: full name of state
                            dataForm.State = Convert.ToString(reader["State"]); //state: initials of the state
    
                            stateList.Add(dataForm);
                        }
                    }
                    con.Close();
                }
                ViewBag.States = stateList;
                return View(); 
            }
    

    这是我的浏览页面

    @model DataEntry.Models.DataForm
    
    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    
    }
    
    @using (Html.BeginForm("DataEntry", "DataForm", new { @class = "form-control", style = "display: flex;" }))
    {
        @Html.AntiForgeryToken()
        <div class="form-group col-md-6">
            <label for="inputEmail4">State: </label>
            @Html.DropDownListFor(m => m.State, new SelectList(ViewBag.States, "State","Description"), "--Please Select--", new { @class = "form-control"})
        </div>
    }
    

    最后,当我尝试运行该项目时,我得到了以下错误。

    微软数据SqlClient。SqlException:“已成功与服务器建立连接,但在登录过程中发生错误。”。(提供程序:SSL提供程序,错误:0-目标主体名称不正确。)'

    Win32Exception:目标主体名称不正确。

    我希望有人能帮我纠正这个错误。非常感谢。

    附言:我没有发布 [HttpPost] 其中一个函数,因为它在添加之前成功地提取了表单 @Html.DropDownListFor(m => m.State, new SelectList(ViewBag.States, "State","Description"), "--Please Select--", new { @class = "form-control"}) 部分。

    0 回复  |  直到 3 年前
        1
  •  2
  •   smbbb    3 年前

    我补充道 Encrypt=False; 在连接字符串的末端,它像一个符咒一样工作。谢谢大家抽出时间来帮助我。

    推荐文章