无法投类型的对象System.Data.SqlClient.SqlDataReader为键入'System.IConvertible“ [英] Unable to cast object of type 'System.Data.SqlClient.SqlDataReader' to type 'System.IConvertible'
问题描述
您好我一直有同样的错误作为标题说。我不知道为什么,什么是错的。我还是新来这个话,我真的不知道如何解决这个问题。如果有人可以帮助我,我会AP preciate它,如果可能的话,请给我提供需要更换一些codeS需要。谢谢你。
公开数据表getAllLoanInfoDT()
{
使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
的SqlCommand CMD2 =新的SqlCommand();
cmd2.Connection =康恩;
// cmd.CommandType = CommandType.StoredProcedure;
cmd2.CommandText =SELECT DISTINCT loanUpdateDate FROM LoanPortfolio WHERE(CUSTID LIKE'OH00002')和(loanType LIKE'个人贷款');
cmd2.Parameters.AddWithValue(@ CUSTID,OH00002);
cmd2.Parameters.AddWithValue(@ loanType,个人贷款);
conn.Open();
SqlDataReader的myReader = cmd2.ExecuteReader();
日期时间loanUpdateDate = Convert.ToDateTime(myReader);
日期时间currDateTime = DateTime.Now; INT loanToBeAdded =(((currDateTime.Year - loanUpdateDate.Year)* 12)+ currDateTime.Month - loanUpdateDate.Month)* 500;
如果(loanToBeAdded大于0)
{
串的SQL =UPDATE LoanPortfolio SET loanPaid = loanPaid ++ loanToBeAdded.ToString()+,loanUpdateDate =+ DateTime.Now.ToString();
SQL + =WHERE(loanType LIKE'个人贷款')和(CUSTID LIKE'OH00002'); //此处执行上面的查询
}
conn.Close(); 使用(SqlDataAdapter的爸爸=新的SqlDataAdapter(SELECT * FROM LoanPortfolio CUSTID哪里像'OH00002',康涅狄格州))
{
数据表dTable =新的DataTable();
dAd.Fill(dTable);
返回dTable;
}
}
}
A SqlDataReader的
是用来读取你的数据 - 你不能只是将它传递到Convert.ToDateTime并希望最好的结果。
首先,你会叫 myReader.Read()
,并检查它返回true(也就是你有一行数据)。
然后,你会叫 Convert.ToDateTime(myReader [0])
来获得的第一个字段出来的数据读取器,并将其转换为日期。
Hi I've been having the same error as the title says. I don't know why what's wrong. I'm still new to this so, I'm not really sure how to solve this. If anyone can help me, I would appreciate it and if possible please provide me some codes that needs to be replaced. Thank you.
public DataTable getAllLoanInfoDT()
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = conn;
// cmd.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "SELECT DISTINCT loanUpdateDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')";
cmd2.Parameters.AddWithValue("@custID", "OH00002");
cmd2.Parameters.AddWithValue("@loanType", "Personal Loan");
conn.Open();
SqlDataReader myReader = cmd2.ExecuteReader();
DateTime loanUpdateDate = Convert.ToDateTime(myReader);
DateTime currDateTime = DateTime.Now;
int loanToBeAdded = (((currDateTime.Year - loanUpdateDate.Year) * 12) + currDateTime.Month - loanUpdateDate.Month) * 500;
if (loanToBeAdded > 0)
{
String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + " + loanToBeAdded.ToString() + ", loanUpdateDate = " + DateTime.Now.ToString();
sql += " WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";
//Execute the above query here
}
conn.Close();
using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM LoanPortfolio where custID like 'OH00002'", conn))
{
DataTable dTable = new DataTable();
dAd.Fill(dTable);
return dTable;
}
}
}
A SqlDataReader
is used to read your data - you can't just pass it into Convert.ToDateTime and hope for the best.
First you would call myReader.Read()
and check it returns true (i.e. you have a row of data).
Then you would call Convert.ToDateTime(myReader[0])
to get the first field out of the data reader and convert it to a date.
这篇关于无法投类型的对象System.Data.SqlClient.SqlDataReader为键入'System.IConvertible“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!