C# 错误无法从“对象”转换为“字符串”

C# 错误无法从“对象”转换为“字符串”,c#,asp.net,C#,Asp.net,我在下面的代码中遇到了一些错误,有人能帮忙吗 错误无法从“object”转换为“string”。与“System.IO.File.Existsstring”匹配的最佳重载方法具有一些无效参数 如何解决这个问题 将读取器[0]替换为: reader.GetString(0) 或 由于读取器[0]用于任意数据,因此类型为object-但该方法需要字符串,因此快速解决方案如下: // note addition of "as string" if (System.IO.File.Exists(rea

我在下面的代码中遇到了一些错误,有人能帮忙吗

错误无法从“object”转换为“string”。与“System.IO.File.Existsstring”匹配的最佳重载方法具有一些无效参数

如何解决这个问题

将读取器[0]替换为:

reader.GetString(0)


由于读取器[0]用于任意数据,因此类型为object-但该方法需要字符串,因此快速解决方案如下:

// note addition of "as string"
if (System.IO.File.Exists(reader[0] as string))

但是,您应该确保该位置的元素确实是字符串。

如果读取器[0]确实是字符串对象,则读取器[0]。ToString将返回字符串值

调试时,您可以通过在即时窗口中调用get type方法来检查读取器[0]的类型


e、 g.reader[0].GetType

实际上,如果按照这种方式,我会使用stringreader[0];如果我们弄错了,异常消息会更清晰-cast vs null arg经常看到这一点。as应该几乎总是跟在空检查之后,从不直接使用。小心。。。你这里有一个SQL注入漏洞。是的,我知道:谢谢
OleDbDataAdapter da = new OleDbDataAdapter("select fact_code,fact_cost,fact_date from factor where fact_date between '" + int.Parse(comboBox2.SelectedItem )+ "/" + int.Parse(comboBox1.SelectedItem) + "/" + int.Parse(comboBox3.SelectedItem) + "'and '" + int.Parse(comboBox5.SelectedItem) + "/" + int.Parse(comboBox4.SelectedItem) + "/" + int.Parse(comboBox6.SelectedItem) + "'", con);
                DataSet dt = new DataSet();
                da.Fill(dt);
                dataGridView1.DataSource = dt.Tables[0];
// note addition of "as string"
if (System.IO.File.Exists(reader[0] as string))
OleDbDataAdapter da = new OleDbDataAdapter("select fact_code,fact_cost,fact_date from factor where fact_date between '" + int.Parse(comboBox2.SelectedItem )+ "/" + int.Parse(comboBox1.SelectedItem) + "/" + int.Parse(comboBox3.SelectedItem) + "'and '" + int.Parse(comboBox5.SelectedItem) + "/" + int.Parse(comboBox4.SelectedItem) + "/" + int.Parse(comboBox6.SelectedItem) + "'", con);
                DataSet dt = new DataSet();
                da.Fill(dt);
                dataGridView1.DataSource = dt.Tables[0];