| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1307 人关注过本帖
标题:类型转换出错!datareader 到datatable
只看楼主 加入收藏
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
 问题点数:0 回复次数:6 
类型转换出错!datareader 到datatable

大家帮我看看这个类,为什么我在我自己机子上不会抛出类型转换出错,而我在我同学机子上就抛出类型转换出错,而且一律的都是数据库中的自增ID字段抛出异常:
public class SystemTools
{
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
//动态添加列
try
{

foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
//添加数据
while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for(int i=0;i<schemaTable.Rows.Count;i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
///抛出类型转换错误
throw new Exception("转换出错出错!",ex);
}

}

这是<ASP.NET网络数据库开发实例精解>那本书办公自动化系统那章里的代码,
我就搞不懂为什么我在自己PC什么问题也没有,但是在我老师PC就会在这个类里出错.而且我把网页发布到网上也可以浏览.地址是http:\\oaauto.vivp.net
下面是出错的地方提示:
Server Error in '/OfficeAuto' Application.
--------------------------------------------------------------------------------

Type of value has a mismatch with column type
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Type of value has a mismatch with column type

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArgumentException: Type of value has a mismatch with column type]
System.Data.Common.ObjectStorage.Set(Int32 recordNo, Object value) +955
System.Data.DataColumn.set_Item(Int32 record, Object value) +37

[ArgumentException: Type of value has a mismatch with column typeCouldn't store <1> in MessageID Column. Expected type is DataRow.]
System.Data.DataColumn.set_Item(Int32 record, Object value) +72
System.Data.DataRow.set_Item(DataColumn column, Object value) +194
System.Data.DataRow.set_Item(Int32 columnIndex, Object value) +25
OfficeAuto.Components.SystemTools.ConvertDataReaderToDataTable(SqlDataReader dataReader) in F:\Documents and Settings\liang\My Documents\Visual Studio Projects\OfficeAuto\Components\SystemTools.cs:40

[Exception: 转换出错出错!]
OfficeAuto.Components.SystemTools.ConvertDataReaderToDataTable(SqlDataReader dataReader) in F:\Documents and Settings\liang\My Documents\Visual Studio Projects\OfficeAuto\Components\SystemTools.cs:52
OfficeAuto.DesktopModules.MessageManage.ViewMsg.BindMsgData(Int32 nUserID) in f:\documents and settings\liang\my documents\visual studio projects\officeauto\desktopmodules\msgmanage\viewmsg.aspx.cs:43
OfficeAuto.DesktopModules.MessageManage.ViewMsg.Page_Load(Object sender, EventArgs e) in f:\documents and settings\liang\my documents\visual studio projects\officeauto\desktopmodules\msgmanage\viewmsg.aspx.cs:35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

[此贴子已经被作者于2007-6-1 12:56:33编辑过]

搜索更多相关主题的帖子: datareader datatable 类型 
2007-06-01 12:45
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
收藏
得分:0 
把try
catch
去掉,在把错误发上来
2007-06-01 12:49
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
我试试看
2007-06-01 12:57
yulphoenix
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2005-10-27
收藏
得分:0 
错误一样
2007-06-01 13:00
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
收藏
得分:0 

try
catch去掉,可以看到详细错误信息

2007-06-01 13:15
铲铲
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:506
专家分:0
注 册:2006-5-2
收藏
得分:0 
DataReader和DataTable之间本就不能转换!

他们完全不兼容,即便追查他们的继承关系也没有任何关联,最终只能追查到他们的超类System.Object

铲铲是也
2007-06-01 20:29
GrimFish
Rank: 1
等 级:新手上路
威 望:1
帖 子:167
专家分:0
注 册:2007-5-28
收藏
得分:0 
...........用DataReader可以填充DataTable,既然DataReader是只读向前,那么用while就可以很简单的得到一个DataTable,估计是LZ没有表述好而已...
2007-06-01 20:41
快速回复:类型转换出错!datareader 到datatable
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015077 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved