zoukankan
html css js c++ java
使用bcp,循环将本地txt文本导入远程sqlserver中
txt大文件导入远程数据库,使用bcp效率极高,关于bcp的资料比较少,写了个导入的方法,在项目中应用成功,代码如下
引用空间:
using
System;
using
System.Data;
using
System.Data.SqlClient;
using
System.Diagnostics;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
string
Conn
=
"
data source=192.168.0.1;initial catalog=Test;user id=sa;password=1
"
;
SqlConnection sqlConn
=
new
SqlConnection(Conn);
SqlCommand cmd
=
new
SqlCommand();
cmd.Connection
=
sqlConn;
SqlDataAdapter sda
=
new
SqlDataAdapter(cmd);
sqlConn.Open();
cmd.CommandText
=
"
Select * from Files
"
;
DataSet ds
=
new
DataSet();
sda.Fill(ds);
if
(ds.Tables[
0
].Rows.Count
>
0
)
{
string
BcpExec
=
""
;
for
(
int
i
=
0
;i
<
ds.Tables[
0
].Rows.Count;i
++
)//循环取本地文件名
{
BcpExec
=
@"
bcp Test..Data in D:\test\
"
;
BcpExec
+=
ds.Tables[
0
].Rows[i][
"
path
"
].ToString();
BcpExec
+=
"
-S192.168.0.1 -Usa -P1 -c -t,
"
;//组合bcp命令
Response.Write(ExeCommand(BcpExec));//执行bcp命令并显示操作结果
}
}
}
/**/
///
<summary>
///
执行Cmd命令
///
确保已经server上已经安装sql,否则使用不了bcp命令,
///
如果没有安装sqlserver需要将bcp.exe拷贝到相应目录(这个条件尚未测试)
///
</summary>
///
<param name="commandText"></param>
///
<returns></returns>
public
static
string
ExeCommand(
string
commandText)
{
Process p
=
new
Process();
p.StartInfo.FileName
=
"
cmd.exe
"
;
p.StartInfo.UseShellExecute
=
false
;
p.StartInfo.RedirectStandardInput
=
true
;
p.StartInfo.RedirectStandardOutput
=
true
;
p.StartInfo.RedirectStandardError
=
true
;
p.StartInfo.CreateNoWindow
=
true
;
string
strOutput
=
null
;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine(
"
exit
"
);
strOutput
=
p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch
(Exception e)
{
strOutput
=
e.Message;
}
return
strOutput;
}
注:ExeCommand函数执行cmd命令是参考
秋枫@Blog
http://blog.csdn.net/zhzuo/archive/2004/12/25/229006.aspx
查看全文
相关阅读:
启动eclipse报错 Could not create the Java Virtual Machine
网站优化-HTML关键词代码使用
[转载]我们应该更依赖手机一点
跨域问题
我研制的操盘机器人今天继续大获全胜2017-03-01
昨日买的股票今日大获全胜,今日买的毫发无损,操盘机器人年后至今仅一天失误,超强,雄起
机器人战胜人类操盘手,我研制三年的成果
年后连续5天开门红,机器人越战越勇
在家附近散个步(新昌),调整一下情绪,明天就要上班了
今天开门红,好兆头
原文地址:https://www.cnblogs.com/cndsn/p/379019.html
最新文章
Ubuntu 开机自动运行一个command 分类: Ubuntu Trick 2015-07-03 23:14 15人阅读 评论(0) 收藏
【转】Using Raycasts and Dynamically Generated Geometry to Create a Line of Sight on Unity3D
【转】unity下的Line of Sight(LOS)的绘制
【转】Unity3D研究院之两种方式播放游戏视频
【转】DontDestroyOnLoad(Unity3D开发之五)
【转】Unity3D研究院之DontDestroyOnLoad的坑
【转】iTween for Unity
【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色
【转】 [Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)
【转】Unity 游戏存档 PlayerPrefs类的用法
热门文章
【转】Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)
WPF调用外部程序
域名系统(DNS)
46种厨房常见调料用法大全
46种厨房常见调料用法大全
[转载]越优秀的男生,对女朋友越好
[转载]越优秀的男生,对女朋友越好
Windows下的Eclipse启动出现:a java runtime environment(JRE) or java&nbs
Windows下的Eclipse启动出现:a java runtime environment(JRE) or java&nbs
启动eclipse报错 Could not create the Java Virtual Machine
Copyright © 2011-2022 走看看