惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
罗磊的独立博客
F
Fortinet All Blogs
T
Threatpost
Y
Y Combinator Blog
博客园_首页
美团技术团队
Security Latest
Security Latest
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
V
V2EX - 技术
The Cloudflare Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
Jina AI
Jina AI
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Hacker News
The Hacker News
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Latest news
Latest news
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
雷峰网
雷峰网
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
W
WeLiveSecurity
D
DataBreaches.Net
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
S
Securelist
Help Net Security
Help Net Security

博客园 - 蕭慶蘋

pymssql调用sqlserver存储过程带output 参数 数据库锁 oracle instr,substr 截取字符串 循环读取写入表 Oracle For 循环,字符串拼接,查找 Putty PSCP iis,webservice 启用 acrobat.exe 打印 iis,webservice 打印 Oracle查询包,同义词 sqlserver 游标 Oracle 截取字符串,取系统时间 - 蕭慶蘋 Sqlserver连接Oracle数据库 Java 连oracle 12C 起步_PreparedStatement Java 连oracle 12C 起步 更改存储过程的所有者 - 蕭慶蘋 jquery-autocomplete 参数说明 了解SQL Server触发器及触发器中的事务 经典SQL语句大全 - 蕭慶蘋 事务使用
powershell excel 导入 sqlserver
蕭慶蘋 · 2015-11-13 · via 博客园 - 蕭慶蘋

powershell excel 导入 sqlserver 更新 多表导入,这之前基础上稍加改动。

cls

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection     #聲明一個SqlConnection對象
$SqlConnection.ConnectionString = "Server=192.168.1.1;Database=sa;user=sa;pwd=123"    #指明SqlConnection對象的連接字符串
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand    #聲明一個SqlCommand對象                                            
$SqlCmd.Connection = $SqlConnection
$excelApp = New-Object -COM Excel.Application
$file = (dir D:\express.xlsx).FullName
$book = $excelApp.Workbooks.Open($file)
$sheet = $book.Worksheets.Item(1)

        $SqlCmd.CommandText="
        INSERT INTO [AppData].[dbo].[ExpressCheckHeader]
               ([CheckId]
               ,[UpdateName]
               ,[UpdateTime]
               ,[HeaderStatus]
               ,[ExpressCompany])
         VALUES
               ('$($sheet.Cells.Item(2,1).Value2)'
               ,'$($sheet.Cells.Item(2,2).Value2)'
               ,'$($sheet.Cells.Item(2,3).Value2)'
               ,0
               ,'$($sheet.Cells.Item(2,4).Value2)')"
              
    try{
        $SqlConnection.Open();
        $intRezult=$SqlCmd.ExecuteNonQuery();
        $SqlConnection.Close();
        $sheet.Cells.Item(2,9).Value2='表头导入成功!';
        "------------------------------------------------------"
        "表头导入成功!"
        "------------------------------------------------------"
    }
    catch
    {
        "表头保存失败!";
        "------------------------------------------------------"
        return;
    }           
           
$row = 5
$count=1 
while($true)
{
    if(!$sheet.Cells.Item($row,1).Value2)
    {
        break;
    } 
      $SqlCmd.CommandText=
      "INSERT INTO [AppData].[dbo].[ExpressCheckLine]
           ([CheckId]
           ,[SendDate]
           ,[ExpressID]
           ,[SendToAddress]
           ,[ExpContent]
           ,[ExpWeight]
           ,[ExpMoney]
           ,[Customer]
           ,[ExpStatus])
     VALUES
           ('$($sheet.Cells.Item($row,1).Value2)'
           ,'$($sheet.Cells.Item($row,2).Value2)'
           ,'$($sheet.Cells.Item($row,3).Value2)'
           ,'$($sheet.Cells.Item($row,4).Value2)'
           ,'$($sheet.Cells.Item($row,5).Value2)'
           ,$($sheet.Cells.Item($row,6).Value2)
           ,$($sheet.Cells.Item($row,7).Value2)
           ,'$($sheet.Cells.Item($row,8).Value2)'
           ,0)"
    
    try{
        $SqlConnection.Open();
        $intRezult=$SqlCmd.ExecuteNonQuery();
        $SqlConnection.Close();
        $sheet.Cells.Item($row,9).Value2='导入成功!';
        $count.toString()+' 条保存成功,条码号:'+$($sheet.Cells.Item($row,3).Value2);
    }
    catch
    {
        $count.toString()+' 条保存失败,条码号:'+$($sheet.Cells.Item($row,3).Value2);
    }
    $count++
    $row++
}
"------------------------------------------------------"
"导入操作完成!请查看导入EXCEL文件!"
"------------------------------------------------------"

$book.Save()
$book.Close()
$excelApp.Quit()
$book = $null
$sheet = $null
$excelApp = $null
[GC]::Collect()