C# winform 读取并执行sqlserver脚本,报GO附加有语法错误
在winform中需要执行sqlserver脚本来创建数据库,用streamreader读取,SqlCommand执行,但总提示go附加有语法错误,如果去掉go的话,语句执行不了,提示找不到AA数据库,sql语句如下:
USE master
GO
CREATE DATABASE AA
ON
( NAME = StoreManager_dat,
FILENAME = 'D:\StoreManager.mdf')
LOG ON
( NAME = 'StoreManager_log',
FILENAME = 'D:\StoreManager_log.ldf');
GO
USE AA
GO
[解决办法]
sqlcommand中的脚本应该不支持Go
试一试
string[] commands = sql.Split( new string[]{"GO\r\n", "GO ", "GO\t"}, StringSplitOptions.RemoveEmptyEntries ); foreach (string c in commands) { command = new SqlCommand(c, masterConnection); command.ExecuteNonQuery(); } } catch (Exception e) { MessageBox.Show(e.Message); } finally { masterConnection.Close(); }}