错误26无法隐式转换类型'对象'到'字符串'。显式转换存在(是否缺少强制转换?) [英] Error 26 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
问题描述
我得到以下错误的错误26无法隐式转换类型
'对象'到'字符串'。一个显式转换存在(是否缺少
CAST)在此行字符串str = RD [0]的ToString()?;当尝试获取文件路径字符串谁能理清我的
问题
在此先感谢
尝试
{
使用(SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [DefaultConnection]。的ToString()))
{
CMD的SqlCommand =新的SqlCommand(); 查询字符串=从[DBO]中选择[PDFFilePath] [AdmPDFManage]其中[PdfId] =(SELECT MAX([PdfId])FROM [DBO] [AdmPDFManage])。
cmd.Connection = CON;
cmd.CommandText =查询;
con.Open();
SqlDataReader的RD =新SqlDataReader的();
而(rd.Read())
{
字符串str = RD [0]的ToString(); }
}
}
赶上(异常前)
{
扔;
}
我也尝试这一点,但得到同样的错误。
块引用>使用(SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [DefaultConnection]。的ToString()))
{
CMD的SqlCommand =新的SqlCommand(); 查询字符串=从[DBO]中选择[PDFFilePath] [AdmPDFManage]其中[PdfId] =(SELECT MAX([PdfId])FROM [DBO] [AdmPDFManage])。
cmd.Connection = CON;
cmd.CommandText =查询;
con.Open(); 字符串PDFfilePath =(字符串)cmd.ExecuteScalar(); }
解决方案我无法重现的问题;它应该工作的罚款。有,但是,两个问题:
- 创建
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 itemThe 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();
这篇关于错误26无法隐式转换类型'对象'到'字符串'。显式转换存在(是否缺少强制转换?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!