有关于大数据量导入导出的工具BCP的具体语法,可以参见SQLServer的帮助文档
下面是园子里的文章
http://www.cnblogs.com/chenbg2001/archive/2010/01/31/1660425.html
我主要是用编程的方式实现
代码
/// <summary>
/// 写入全部数据到txt文件
/// </summary>
/// <param name="fileName">txt文件全路劲</param>
public void WriteAllToFile(string fileName)
{
List<string> queryString = new List<string>();
//开启xp_cmdshell
string xp_cmdshellOpen1 = "EXEC sp_configure 'show advanced options', 1;RECONFIGURE;";
string xp_cmdshellOpen2 = "EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;";
//导出数据String为bcp SQLServer2005DB.dbo.test out e:\test.txt -c -U sa -P sa123 -S ET07\SQLEXPRESS
string[] messageInfo = PubConstant.MessageInfo();
string serverName = messageInfo[0];
string dbName = messageInfo[1];
string userName = messageInfo[2];
string password = messageInfo[3];
string exportStr =
string.Format("exec {0}..xp_cmdshell 'bcp {1}.dbo.Department out {2} -c -U {3} -P {4} -S {5}'", dbName, dbName, fileName, userName, password, serverName);
//关闭xp_comshell
string xp_cmdshellClose1 = "EXEC sp_configure 'show advanced options', 1;RECONFIGURE;";
string xp_cmdshellClose2 = "EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;";
queryString.Add(xp_cmdshellOpen1);
queryString.Add(xp_cmdshellOpen2);
queryString.Add(exportStr);
queryString.Add(xp_cmdshellClose1);
queryString.Add(xp_cmdshellClose2);
foreach (string item in queryString)
{
DbHelperSQL.ExecuteSql(item);
}
}
/// 写入全部数据到txt文件
/// </summary>
/// <param name="fileName">txt文件全路劲</param>
public void WriteAllToFile(string fileName)
{
List<string> queryString = new List<string>();
//开启xp_cmdshell
string xp_cmdshellOpen1 = "EXEC sp_configure 'show advanced options', 1;RECONFIGURE;";
string xp_cmdshellOpen2 = "EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;";
//导出数据String为bcp SQLServer2005DB.dbo.test out e:\test.txt -c -U sa -P sa123 -S ET07\SQLEXPRESS
string[] messageInfo = PubConstant.MessageInfo();
string serverName = messageInfo[0];
string dbName = messageInfo[1];
string userName = messageInfo[2];
string password = messageInfo[3];
string exportStr =
string.Format("exec {0}..xp_cmdshell 'bcp {1}.dbo.Department out {2} -c -U {3} -P {4} -S {5}'", dbName, dbName, fileName, userName, password, serverName);
//关闭xp_comshell
string xp_cmdshellClose1 = "EXEC sp_configure 'show advanced options', 1;RECONFIGURE;";
string xp_cmdshellClose2 = "EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;";
queryString.Add(xp_cmdshellOpen1);
queryString.Add(xp_cmdshellOpen2);
queryString.Add(exportStr);
queryString.Add(xp_cmdshellClose1);
queryString.Add(xp_cmdshellClose2);
foreach (string item in queryString)
{
DbHelperSQL.ExecuteSql(item);
}
}
//开启xp_cmdshell与关闭xp_cmdshell,是为了方便执行bcp命令行的。使用完之后关闭命令。
然后就是执行sql语句,DbHelperSQL在我上一篇随笔中有源码。就是简单的执行sql的帮助类。
用bcp工具的好处是,备份大数据量的数据比较快。具体你可以做单元测试测试一番。
接下来要介绍的是一个很好的工具,OPENROWSET
也是SQLServer2005的工具,可以直接读取Access数据库,或者直接连接远程的数据库服务器。
下面是代码实现。
代码
/// <summary>
/// 直接读取Access数据库
/// </summary>
/// <param name="fileName"></param>
public void ReadFromAccess(string fileName)
{
List<string> queryString = new List<string>();
//开启
string xp_cmdshellOpen1 = "exec sp_configure 'show advanced options',1 reconfigure";
string xp_cmdshellOpen2 = "exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure";
string exportStr = @"SELECT CustomerID, CompanyName FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','C:\testFile\Northwind.mdb';'admin';'',Customers)";
//关闭
string xp_cmdshellClose1 = "exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure";
string xp_cmdshellClose2 = "exec sp_configure 'show advanced options',0 reconfigure";
queryString.Add(xp_cmdshellOpen1);
queryString.Add(xp_cmdshellOpen2);
queryString.Add(exportStr);
queryString.Add(xp_cmdshellClose1);
queryString.Add(xp_cmdshellClose2);
foreach (string item in queryString)
{
DbHelperSQL.ExecuteSql(item);
}
}
/// 直接读取Access数据库
/// </summary>
/// <param name="fileName"></param>
public void ReadFromAccess(string fileName)
{
List<string> queryString = new List<string>();
//开启
string xp_cmdshellOpen1 = "exec sp_configure 'show advanced options',1 reconfigure";
string xp_cmdshellOpen2 = "exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure";
string exportStr = @"SELECT CustomerID, CompanyName FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','C:\testFile\Northwind.mdb';'admin';'',Customers)";
//关闭
string xp_cmdshellClose1 = "exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure";
string xp_cmdshellClose2 = "exec sp_configure 'show advanced options',0 reconfigure";
queryString.Add(xp_cmdshellOpen1);
queryString.Add(xp_cmdshellOpen2);
queryString.Add(exportStr);
queryString.Add(xp_cmdshellClose1);
queryString.Add(xp_cmdshellClose2);
foreach (string item in queryString)
{
DbHelperSQL.ExecuteSql(item);
}
}