首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

用SqlBulkCopy批量安插数据时提示来自数据源的 String 类型的给定值不能转换为指定目标列的类型 int

2013-05-02 
用SqlBulkCopy批量插入数据时提示来自数据源的 String 类型的给定值不能转换为指定目标列的类型 int。DataT

用SqlBulkCopy批量插入数据时提示来自数据源的 String 类型的给定值不能转换为指定目标列的类型 int。

DataTable dt = new DataTable();
            dt.Columns.Add("greouparea", typeof(System.Int32));
            dt.Columns.Add("grouptype", typeof(System.Int32));
            dt.Columns.Add("name", typeof(System.String));
            dt.Columns.Add("mainperson", typeof(System.String));
            dt.Columns.Add("groupform", typeof(System.String));
            dt.Columns.Add("scale", typeof(System.String));
            dt.Columns.Add("nature", typeof(System.Int32));
            dt.Columns.Add("description", typeof(System.String));
            dt.Columns.Add("level", typeof(System.Int32));
            dt.Columns.Add("groupuser", typeof(System.String));
            dt.Columns.Add("reportedsource", typeof(System.Int32));
            dt.Columns.Add("localid", typeof(System.Int32));
            dt.Columns.Add("userid", typeof(System.Int32));
            dt.Columns.Add("remark", typeof(System.String));
            DataRow dr = dt.NewRow();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = dt.NewRow();
                dr["name"] = ds.Tables[0].Rows[i]["组织名称"].ToString();
                dr["nature"] = param.id;
                dr["reportedsource"] = local.id;
                dr["localid"] = local.id;
                dr["level"] = Model.id;
                dr["scale"] = ds.Tables[0].Rows[i]["组织规模"].ToString();
                dr["greouparea"] = ds.Tables[0].Rows[i]["组织地域"].ToString() == "境内组织" ? 10 : 11;
                dr["grouptype"] = ds.Tables[0].Rows[i]["组织类型"].ToString() == "网络组织 " ? 0 : 1;
                dr["mainperson"] = ds.Tables[0].Rows[i]["组织负责人"].ToString();
                dr["groupform"] = ds.Tables[0].Rows[i]["组织形式"].ToString();


                dr["description"] = ds.Tables[0].Rows[i]["组织描述"].ToString();
                dr["groupuser"] = ds.Tables[0].Rows[i]["组织成员"].ToString();
                dr["userid"] = AMSession.LoginUser.id;
                dr["remark"] = ds.Tables[0].Rows[i]["备注信息"].ToString();
                dt.Rows.Add(dr);
            }

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(AMCommon.DBUtility.DbHelperSQL.connectionString);
            sqlBulkCopy.DestinationTableName = "groupinfo";
            sqlBulkCopy.BatchSize = dt.Rows.Count;

            SqlConnection sqlConnection = new SqlConnection(AMCommon.DBUtility.DbHelperSQL.connectionString);
            sqlConnection.Open();
            if (dt != null && dt.Rows.Count != 0)
            {
                sqlBulkCopy.WriteToServer(dt);
            }
            sqlBulkCopy.Close();
            sqlConnection.Close();
            stopwatch.Stop();


[解决办法]
SqlBulkCopy不是根据表的ColumnName来匹配的,而是根据ColumnIndex匹配,
也就是说你的表 字段必须跟数据库的表字段完全一致(Index的排序要跟数据表的一样)。
就算你该字段不打算给他插入值,也要建个DataColumn。包括自增ID.不需要给他值就好了

热点排行