且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误26无法隐式转换类型'对象'到'字符串'。显式转换存在(是否缺少强制转换?)

更新时间:2023-02-13 08:32:06

我无法重现的问题;它应该工作的罚款。有,但是,两个问题:


  • 创建 SqlDataReader的错误

  • 不使用 .ConnectionString 从配置项

以下编译罚款:

 使用(VAR CON =新的SqlConnection(
    ConfigurationManager.ConnectionStrings [DefaultConnection]。的ConnectionString))
使用(VAR CMD = con.CreateCommand())
{
    常量字符串查询=选择[PDFFilePath]从[DBO] [AdmPDFManage]其中[PdfId] =(SELECT MAX([PdfId])FROM [DBO] [AdmPDFManage])。
    cmd.CommandText =查询;
    con.Open();
    使用(VAR RD = cmd.ExecuteReader())
    {
        而(rd.Read())
        {
            字符串str = RD [0]的ToString();
            // .. 做一点事
        }
    }
}

请注意,如果你只希望1列,的ExecuteScalar 是简单的:

  cmd.CommandText =查询;
con.Open();
字符串str =(字符串)cmd.ExecuteScalar();

I get the following error Error 26 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) ON THIS LINE string str = rd[0].ToString(); when try to get filepath in string can anyone sort out my problem

thanks in advance

try
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
    {
        SqlCommand cmd = new SqlCommand();

        String query = "select [PDFFilePath] from [dbo].[AdmPDFManage] Where [PdfId] = (SELECT MAX([PdfId]) FROM [dbo].[AdmPDFManage]) ";
        cmd.Connection = con;
        cmd.CommandText = query;
        con.Open();
        SqlDataReader rd = new SqlDataReader();
        while (rd.Read())
        {
            string str = rd[0].ToString();

        }
    }
}
catch (Exception ex)
{
    throw;
}

I ALSO TRY THIS BUT GET THE SAME ERROR

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
            {
                SqlCommand cmd = new SqlCommand();

                String query = "select [PDFFilePath] from [dbo].[AdmPDFManage] Where [PdfId] = (SELECT MAX([PdfId]) FROM [dbo].[AdmPDFManage]) ";
                cmd.Connection = con;
                cmd.CommandText = query;
                con.Open();

                String PDFfilePath = (String)cmd.ExecuteScalar();              

            }

I can't reproduce the issue; it should work fine. There are, however, 2 problems:

  • creating the SqlDataReader incorrectly
  • not using the .ConnectionString from the configuration item

The following compiles fine:

using (var con = new SqlConnection(
    ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
using (var cmd = con.CreateCommand())
{
    const string query = "select [PDFFilePath] from [dbo].[AdmPDFManage] Where [PdfId] = (SELECT MAX([PdfId]) FROM [dbo].[AdmPDFManage]) ";
    cmd.CommandText = query;
    con.Open();
    using(var rd = cmd.ExecuteReader())
    {
        while (rd.Read())
        {
            string str = rd[0].ToString();
            // .. do something
        }
    }
}

Note that if you only expect 1 row, ExecuteScalar is simpler:

cmd.CommandText = query;
con.Open();
string str = (string) cmd.ExecuteScalar();