将值从CSV导入SQL Server

时间:2012-05-29 19:19:43

标签: sql-server powershell csv

我需要将CSV数据从客户端计算机导入到网络上其他位置的SQL Server。我想用PowerShell做这件事,因为我试图更流畅地使用它。

然而,当我使用我在互联网上找到的一个例子时,我遇到了一个我不明白的问题。

作为参考,我使用的是第三个例子:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/28/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell.aspx

我已将信息导入到数据表中,但是看起来一旦系统尝试将数据插入数据库,我就会收到以下错误,这显然是我的数据如何进入我的数据表的问题,因为在符号和括号似乎导致问题。

这是错误:

  

System.Management.Automation.MethodException:无法转换参数“0”,值为:“System.Object []”,“WriteToServer”为   输入“System.Data.DataRow []”:
  “无法转换”@ {[Type] = 1; [EID] = 803; [姓] =鲍勃; [MiddleInit] =; [姓氏] =米勒; [雇佣日期] = 03031903; [规则] = H;   [速率] = 0100; [状态] = 1; [存储] = 8; [部门] = 04; [类别] = 130;   [徽章] = 803;}“类型的值   “System.Management.Automation.PSCustomObject”键入“System.Data.DataRow”。“

  ---> System.Management.Automation.PSInvalidCastException:无法转换   “@ {[Type] = 1; [EID] = 803; [FirstName] = Bob; [MiddleInit] =;   [姓氏] =米勒; [雇佣日期] = 03031903; [规则] = H; [速率] = 0100;   [状态] = 1; [存储] = 8; [部门] = 04; [类别] = 130; [徽章] = 803;}“的价值   键入“System.Management.Automation.PSCustomObject”进行输入   “的System.Data.DataRow”。

     

在System.Management.Automation.LanguagePrimitives.ConvertTo(Object   valueToConvert,Type resultType,Boolean recursion,IFormatProvider   formatProvider,TypeTable backupTypeTable)
  在System.Management.Automation.LanguagePrimitives.ConvertUnrelatedArrays(Object   valueToConvert,Type resultType,Boolean recursion,PSObject   originalValueToConvert,IFormatProvider formatProvider,TypeTable backupTable)
  在System.Management.Automation.LanguagePrimitives.ConvertTo(Object   valueToConvert,Type resultType,Boolean recursion,IFormatProvider   formatProvider,TypeTable backupTypeTable)
  在System.Management.Automation.Adapter.PropertySetAndMethodArgumentConvertTo(Object   valueToConvert,Type resultType,IFormatProvider formatProvider)
  在System.Management.Automation.Adapter.MethodArgumentConvertTo(对象   valueToConvert,Boolean isParameterByRef,Int32 parameterIndex,Type   resultType,IFormatProvider formatProvider)
  在System.Management.Automation.Adapter.SetNewArgument(String methodName,Object [] arguments,Object [] newArguments,   ParameterInformation参数,Int32索引)
  ---内部异常堆栈跟踪结束---   在System.Management.Automation.Adapter.SetNewArgument(String methodName,Object [] arguments,Object [] newArguments,   ParameterInformation参数,Int32索引)
  在System.Management.Automation.Adapter.GetMethodArgumentsBase(String   methodName,ParameterInformation []参数,Object []参数,   Boolean expandParamsOnBest)
  在System.Management.Automation.DotNetAdapter.MethodInvokeDotNet(String   methodName,Object target,MethodInformation [] methodInformation,   Object []参数)
  在System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod方法,   Object []参数)
  at System.Management.Automation.ParserOps.CallMethod(Token token,Object target,String methodName,Object [] paramArray,Boolean   callStatic,Object valueToSet)
  在System.Management.Automation.MethodCallNode.InvokeMethod(Object   target,Object []参数,对象值)
  at System.Management.Automation.MethodCallNode.Execute(Array input,Pipe outputPipe,ExecutionContext context)
  at System.Management.Automation.ParseTreeNode.Execute(Array input,Pipe outputPipe,ArrayList& resultList,ExecutionContext context)
  在System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode   statement,Array input,Pipe outputPipe,ArrayList& resultList,   ExecutionContext上下文)
  消息+ CategoryInfo:未指定:(:) [Write-Error],WriteErrorException   + FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,Write-DataTable.PS1

0 个答案:

没有答案