我试图将xml读入SQL并在“ds.ReadXml(”C:\ test.xml“)中获取错误;”线, 关于什么错误的任何暗示?感谢
tring connectionString = "Data Source=MyServer\\SQLEXPRESS;Initial Catalog=MyCatalog;Password=xxx;Persist Security Info=True;User ID=xxx";
DataSet ds = new DataSet();
DataTable sourcedata = new DataTable();
ds.ReadXml("C:\test.xml");
sourcedata = ds.Tables[0];
using (SqlConnection sqlconn = new SqlConnection(connectionString))
{
sqlconn.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconn))
{
bulkCopy.ColumnMappings.Add("ID", "ID");
bulkCopy.ColumnMappings.Add("user_id", "user_id");
bulkCopy.ColumnMappings.Add("upload_date", "upload_date");
bulkCopy.ColumnMappings.Add("userAddr", "userAddr");
bulkCopy.ColumnMappings.Add("serial_no", "serial_no");
bulkCopy.ColumnMappings.Add("Mfgr", "Mfgr");
bulkCopy.ColumnMappings.Add("model", "model");
bulkCopy.ColumnMappings.Add("description", "description");
bulkCopy.ColumnMappings.Add("type", "type");
bulkCopy.ColumnMappings.Add("code", "code");
bulkCopy.ColumnMappings.Add("comments", "comments");
bulkCopy.ColumnMappings.Add("qty", "qty");
bulkCopy.ColumnMappings.Add("condition", "condition");
bulkCopy.ColumnMappings.Add("location", "location");
bulkCopy.ColumnMappings.Add("price", "price");
bulkCopy.DestinationTableName = "Upload";
bulkCopy.WriteToServer(sourcedata);
}
}
}
答案 0 :(得分:5)
您的C:\test.xml
被视为C:(tab character)est.xml
。在字符串前加一个@:@"C:\test.xml"
,或者用"C:\\test.xml"
转义它。
答案 1 :(得分:4)
尝试转义'\'符号:
ds.ReadXml("C:\\test.xml");
答案 2 :(得分:3)
尝试:
ds.ReadXml(@"C:\test.xml");
\ t被视为水平制表符,除非你将其转义。
答案 3 :(得分:2)
尝试更改此内容:
ds.ReadXml("C:\test.xml");
与
ds.ReadXml("C:\\test.xml");
答案 4 :(得分:2)
您需要转义\
,例如:ds.ReadXml("C:\\test.xml");
或ds.ReadXml(@"C:\test.xml");
答案 5 :(得分:1)
你的问题是你的\字符需要转义。目前它被读作\t
这是一个制表符并且无效。你想要:
ds.ReadXml(@"C:\test.xml");
ds.ReadXml("C:\\test.xml");
答案 6 :(得分:0)
答案 7 :(得分:0)
希望您的xml文件格式正确。您只需使用 ds.ReadXml(@“C:\ test.xml”); 内容为 ds.ReadXml(“C:\ test.xml”);