如何解决此错误: - 数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型。 [英] How to resolve this error:- The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.?
问题描述
您好,
如何解决此错误: - 数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型。 ?
我使用此代码导入批量上传表。:
string excelConnectionString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ RoofColorBulkUploadPath +; Extended Properties = Excel 8.0;
//创建与Excel工作簿的连接
使用(OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand(Select * FROM [RoofColour $] ,连接);
connection.Open();
//使用(System.Data.Common.DbDataReader)创建DbDataReader到数据工作表
dr = command.ExecuteReader())
{
// SQL Server连接字符串
字符串sqlConnectionString = cnString1;
//批量复制到SQL Server
使用(SqlBulkCopy bulkCopy =
新的SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName =RoofColour;
bulkCopy.WriteToServer(dr); //错误: - 数据源中String类型的给定值无法转换为指定目标的typeideideier类型列。
dr.Close();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.SelectCommand = comm和;
da.Fill(dt);
if(dt.Rows.Count> 1)
{
SqlConnection con1 = new SqlConnection(cnString1);
con1.Open();
dadapter = new SqlDataAdapter(SELECT * FROM [RoofColour],con1);
data = new DataTable();
dadapter.Fill(data);
dataGridView1.DataSource = data;
con1.Close();
MessageBox.Show(数据导入到RoofColour成功。);
}
}
}
请帮帮我。
先谢谢。
Ankit Agarwal
软件工程师
Hello,
How to resolve this error:- The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.?
I am using this code for import bulk upload sheet.:-
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RoofColorBulkUploadPath + " ;Extended Properties=Excel 8.0";
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [RoofColour$]", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (System.Data.Common.DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = cnString1;
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "RoofColour";
bulkCopy.WriteToServer(dr);//Error:- The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.
dr.Close();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.SelectCommand = command;
da.Fill(dt);
if (dt.Rows.Count > 1)
{
SqlConnection con1 = new SqlConnection(cnString1);
con1.Open();
dadapter = new SqlDataAdapter("SELECT * FROM [RoofColour]", con1);
data = new DataTable();
dadapter.Fill(data);
dataGridView1.DataSource = data;
con1.Close();
MessageBox.Show("Data Imported to RoofColour Successfully.");
}
}
}
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
推荐答案
,连接);
connection.Open();
//创建DbDataReader到数据工作表
使用(System.Data.Common.DbDataReader dr = command.ExecuteReader())
{
/ / SQL Server连接字符串
字符串sqlConnectionString = cnString1;
//批量复制到SQL Server
使用(SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName =RoofColour;
bulkCopy.WriteToServer(dr); //错误: - 数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型。
dr.Close();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.SelectCommand = command;
da.Fill(dt);
if(dt.Rows.Count> 1)
{
SqlConnection con1 = new SqlConnection(cnString1);
con1.Open();
dadapter = new SqlDataAdapter(SELECT * FROM [RoofColour],con1);
data = new DataTable();
dadapter.Fill(data);
dataGridView1.DataSource = data;
con1.Close();
MessageBox.Show(数据导入到RoofColour成功。);
}
}
}
请帮帮我。
在此先感谢。
Ankit Agarwal
软件工程师
", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (System.Data.Common.DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = cnString1;
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "RoofColour";
bulkCopy.WriteToServer(dr);//Error:- The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.
dr.Close();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.SelectCommand = command;
da.Fill(dt);
if (dt.Rows.Count > 1)
{
SqlConnection con1 = new SqlConnection(cnString1);
con1.Open();
dadapter = new SqlDataAdapter("SELECT * FROM [RoofColour]", con1);
data = new DataTable();
dadapter.Fill(data);
dataGridView1.DataSource = data;
con1.Close();
MessageBox.Show("Data Imported to RoofColour Successfully.");
}
}
}
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
我认为数据库中的RoofColour表包含一个数据类型为uniqueidentifier的列,excel表没有t包含guid之类的值(例如。 '457596b3-1f1d-4748-9286-7edb38127f33')你要导入它。
I think RoofColour table in database contains a column with datatype as uniqueidentifier and excel sheet does not contains the value like guid (eg. '457596b3-1f1d-4748-9286-7edb38127f33') in it that you are importing.
这篇关于如何解决此错误: - 数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!