我正在开发一个 C# VS 2008 / SQL Server 网站应用程序。我是 ASP.NET 的新手。但是,在以下代码的最后一行,我收到了上述错误。你能给我建议如何解决这个问题吗?这编译正确,但运行后我遇到了这个错误。
我要做的就是将“dt”第二行中的项目存储到字符串参数中。第一行是标题,所以我不想要这些值。第二行是第一行值。我的 SQL 存储过程需要这些值作为字符串。所以我想解析第二行数据并加载到2个字符串参数中。我在下面添加了更多代码。
DataTable dt;
Hashtable ht;
string[] SingleRow;
...
SqlConnection conn2 = new SqlConnection(connString);
SqlCommand cmd = conn2.CreateCommand();
cmd.CommandText = "dbo.AppendDataCT";
cmd.Connection = conn2;
SingleRow = (string[])dt.Rows[1].ItemArray;
SqlParameter sqlParam = cmd.Parameters.AddWithValue("@" + ht[0], SingleRow[0]);
sqlParam.SqlDbType = SqlDbType.VarChar;
SqlParameter sqlParam2 = cmd.Parameters.AddWithValue("@" + ht[1], SingleRow[1]);
sqlParam2.SqlDbType = SqlDbType.DateTime;
我的错误:
System.InvalidCastException was caught
Message="Unable to cast object of type 'System.Object[]' to type 'System.String[]'."
Source="App_Code.g68pyuml"
StackTrace:
at ADONET_namespace.ADONET_methods.AppendDataCT(DataTable dt, Hashtable ht) in c:\Documents and Settings\Admin\My Documents\Visual Studio 2008\WebSites\Jerry\App_Code\ADONET methods.cs:line 88
InnerException: