加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 尝试恢复数据库中列的当前值时出错

发布时间:2020-12-15 21:44:58 所属栏目:百科 来源:网络整理
导读:我的表中有一个名为AllowComent的列.该列具有位数据类型. 在我的C#代码中,我尝试恢复该值 protected string RecoverAllowComent(string id){ try { using(SqlConnection conexao = new SqlConnection(_conexao)) { SqlCommand comando = new SqlCommand(); c
我的表中有一个名为AllowComent的列.该列具有位数据类型.

在我的C#代码中,我尝试恢复该值

protected string RecoverAllowComent(string id)
{
    try
    {
        using(SqlConnection conexao = new SqlConnection(_conexao))
        {
            SqlCommand comando = new SqlCommand();
            comando.CommandText = "SELECT AllowComent FROM San_Blog WHERE Id = @Id";
            comando.Parameters.Add(new SqlParameter("@Id",id));
            comando.CommandType = CommandType.Text;
            comando.Connection = conexao;
            conexao.Open();
            comando.ExecuteNonQuery();

            SqlDataAdapter da = new SqlDataAdapter(comando);
            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0].ItemArray[0].ToString();
            }
            else                    
                return "False";
         }
     }
     catch
     {
         return "False";
     }
}

但我得到了错误
将nvarchar值“1”转换为数据类型int时转换失败.

解决方法

您正在传递具有类型字符串的传递ID.您需要将int传递给参数id.

更改

comando.Parameters.Add(new SqlParameter("@Id",id));

comando.Parameters.Add(new SqlParameter("@Id",int.Parse(id)));

如果可能,您可以将RecoverAllowComent的参数ID类型更改为int,以便从类型转换字符串转换为int.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读