移植" SQL"出口T-SQL
我有一个第三方应用程序,并数据库中导出到SQL格式这是相当straitforward但它似乎没有使用T-SQL完全吻合。当我出口它产生这样的事情
I have a 3rd party app that does a database export in to a "SQL format" it is fairly straitforward however it appears to not use T-SQL exactly. When I export it produce something like this
INSERT INTO [ExampleDB] ( [IntField] , [DateTimeField] , [VarcharField], [BinaryField])
VALUES
(1 , '2012/04/02 12:25:00:01' , 'Some Text' , X'123456'),
(2 , '0000/00/00 00:00:00:00' , 'B' , NULL),
--(SNIP, it does this for 1000 records)
(999,'0000/00/00 00:00:00:00' , 'Other Text' , null);
(1000 ,'0000/00/00 00:00:00:00' , 'D' , null);
INSERT INTO [ExampleDB] ( [IntField] , [DateTimeField] , [VarcharField] , BinaryField)
VALUES
(1001 , '2012/04/02 12:25:00:01' , 'Trying to break my parser with the next line', null),
(1002 , '0000/00/00 00:00:00:00' , ' ''X''123' , X'deadbeef'),
(1003 , '0000/00/00 00:00:00:00' , 'Did it break it?' , null),
(1004 , '2012/04/02 12:25:00:01' , 'What about this? ''0000/00/00 00:00:00:00'' Will that?' , null),
--(SNIP)
这两个断枝我运行中有:
The two snags I am running in to are:
-
'0000/00/00 00:00:00:00的值
的日期 - 它们存储二进制输出的X前缀字符串的事实
- the value of
'0000/00/00 00:00:00:00'
for a date - the fact that they store binary output as a string prefixed by X
我想我可以做一个 Regex.Replace
就可以了,但我想知道如果有一个简单的解决方案。到目前为止,我的代码导入为
I am thinking I can just do a Regex.Replace
on it, but I am wondering if there is a easier solution. So far my code to import is
using (var cmd = new SqlCommand("", conn))
{
//snip
cmd.CommandTimeout = 0; //Wait forever, bad bad bad, but what else can I do?
using (var txtRdr = new StreamReader(file))
{
string query = txtRdr.ReadToEnd();
query = query.Replace(" '0000/00/00 00:00:00:00' ", " NULL ");
query = Regex.Replace(query, "X'([0-9a-fA-F]+)'", @"0x$1");
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
}
由于他们的方式列出值我知道我绑SQL2008或更新,但有什么陷阱我写使其解析,能够通过SQL2008的代码?
Because the way they list values I know I am tied to SQL2008 or newer but are there any gotchas to the code I wrote to make it parse-able by SQL2008?
当我运行它,它就会越陷越深在执行查询。 SQL文件是(但我可以调整),8000 KB的大小有什么我可以做,以加快步伐?也许做一些更多的格式,并把它在一个批量插入?
When I run it it gets bogged down on the Execute query. The SQL files are 8000 KB in size (but I can adjust it) is there anything I can do to speed it up? Perhaps do some more formatting and turn it in to a bulk insert?
每当我有两个数据库之间传输数据I已经发现,最快捷的方法似乎是如下:
Whenever I have had to transfer data between two databases I have found that the quickest way seems to be as follows:
- 使用卸载或同等功能来批量出口源数据库到一个文本文件
- 编写软件的文本文件格式从源格式转换为目标格式转换。
- 使用批量导入或类似功能加载文本文件到目标数据库中。