日期:2014-05-19  浏览次数:20669 次

请问怎样来读取XXX.sql文件中的sql并且执行里面的sql
又没有类似的例子?

------解决方案--------------------
//读取 StreamReader sr = new StreamReader(@ "d:\**.sql ");
string strsql = sr.ReadToEnd();
执行在连接数据库后 sqlcmd.CommandText = strsql;执行就可以了


------解决方案--------------------
this solution won 't work if the script contains go statement.

start a new process call osql to run the script
------解决方案--------------------
osql 实用工具
osql 实用工具使您得以输入 Transact-SQL 语句、系统过程和脚本文件。该实用工具通过 ODBC 与服务器通讯。

语法
osql
[-?] |
[-L] |
[
{
{-U login_id [-P password]}
| -E
}
[-S server_name[\instance_name]] [-H wksta_name] [-d db_name]
[-l time_out] [-t time_out] [-h headers]
[-s col_separator] [-w column_width] [-a packet_size]
[-e] [-I] [-D data_source_name]
[-c cmd_end] [-q "query "] [-Q "query "]
[-n] [-m error_level] [-r {0 | 1}]
[-i input_file] [-o output_file] [-p]
[-b] [-u] [-R] [-O]
]

sql内置工具,.sql文件中可以今有[go]之类的非sql标准语句,适合在安装程序调用
------解决方案--------------------
Process p=new Process();
p.StartupInfo.FileName= "osql.exe ";

.
.
.

p.Start();