使用bcp,循环将本地txt文本导入远程sqlserver中

txt大文件导入远程数据库,使用bcp效率极高,关于bcp的资料比较少,写了个导入的方法,在项目中应用成功,代码如下
引用空间:
None.gifusing  System;
None.gif
using
 System.Data;
None.gif
using
 System.Data.SqlClient;
None.gif
using System.Diagnostics;

None.gifprivate void Page_Load(object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            
// 在此处放置用户代码以初始化页面

InBlock.gif
            string Conn = "data source=192.168.0.1;initial catalog=Test;user id=sa;password=1";
InBlock.gif            SqlConnection sqlConn 
= new
 SqlConnection(Conn);
InBlock.gif            SqlCommand cmd 
= new
 SqlCommand();
InBlock.gif            cmd.Connection 
=
 sqlConn;
InBlock.gif            SqlDataAdapter sda 
= new
 SqlDataAdapter(cmd);
InBlock.gif            sqlConn.Open();
InBlock.gif            cmd.CommandText 
= "Select * from Files"
;
InBlock.gif            DataSet ds 
= new
 DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif
InBlock.gif            
if(ds.Tables[0].Rows.Count>0
)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif                
string BcpExec = ""
;
InBlock.gif                
for(int i=0;i<ds.Tables[0].Rows.Count;i++
)//循环取本地文件名
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
{            
InBlock.gif                    BcpExec 
= @"bcp Test..Data in D:\test\"
;
InBlock.gif                    BcpExec 
+= ds.Tables[0].Rows[i]["path"
].ToString();
InBlock.gif                    BcpExec 
+= " -S192.168.0.1 -Usa -P1 -c -t,"
;//组合bcp命令
InBlock.gif                    Response.Write(ExeCommand(BcpExec));//执行bcp命令并显示操作结果
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 执行Cmd命令
InBlock.gif        
///
 确保已经server上已经安装sql,否则使用不了bcp命令,
InBlock.gif        
///
 如果没有安装sqlserver需要将bcp.exe拷贝到相应目录(这个条件尚未测试)
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="commandText"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif        public static string ExeCommand(string  commandText)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            Process p 
= new
 Process();
InBlock.gif            p.StartInfo.FileName 
= "cmd.exe"
;
InBlock.gif            p.StartInfo.UseShellExecute 
= false
;
InBlock.gif            p.StartInfo.RedirectStandardInput 
= true
;
InBlock.gif            p.StartInfo.RedirectStandardOutput 
= true
;
InBlock.gif            p.StartInfo.RedirectStandardError 
= true
;
InBlock.gif            p.StartInfo.CreateNoWindow 
= true
;
InBlock.gif            
string strOutput = null
;
InBlock.gif            
try

ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                p.Start();
InBlock.gif                p.StandardInput.WriteLine(commandText);
InBlock.gif                p.StandardInput.WriteLine(
"exit"
);
InBlock.gif                strOutput 
=
 p.StandardOutput.ReadToEnd();
InBlock.gif                p.WaitForExit();
InBlock.gif                p.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif                strOutput 
=
 e.Message;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strOutput;
ExpandedBlockEnd.gif        }

注:ExeCommand函数执行cmd命令是参考 秋枫@Blog   http://blog.csdn.net/zhzuo/archive/2004/12/25/229006.aspx

转载于:https://www.cnblogs.com/cndsn/archive/2006/04/19/379019.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值