我有以下场景:我使用PowerShell并且必须将.csv文件中的数据导入到SQL Server数据库上已创建的表中.所以我不需要csv的标题行,只需写入数据.
这是我到目前为止所做的:
#Setup for SQL Server connection
#SQL Server Name
$SQLServer = "APPLIK02\SQLEXPRESS"
#Database Name
$SQLDBName = "code-test"
#Create the SQL Connection Object
$SQLConn = New-Object System.Data.SQLClient.SQLConnection
#Create the SQL Command Object, to work with the Database
$SQLCmd = New-Object System.Data.SQLClient.SQLCommand
#Set the connection string one the SQL Connection Object
$SQLConn.ConnectionString = "Server=$SQLServer;Database=$SQLDBName; Integrated Security=SSPI"
#Open the connection
$SQLConn.Open()
#Handle the query with SQLCommand Object
$SQLCmd.CommandText = $query
#Provide the open connection to the Command Object as a property …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用数字和字母数字值导入列(来自.csv文件),但是当我运行该openrowset过程时,它会正确导入数字行,但对于字母数字值,它默认为null.
表A.
ID,A,B,C
1,12,hh,i
2,ab12,tt,b
3,2,aa,o
4,bc12,ee,l
Run Code Online (Sandbox Code Playgroud)
使用的代码
SELECT
*
FROM
OPENROWSET
(
'Microsoft.ACE.OLEDB.12.0','Text;Database=C:\;IMEX=1;','SELECT * FROM abc.csv'
) t
Run Code Online (Sandbox Code Playgroud)
我用过IMEX =1,根本没有变化.