Hi,
The following code does not work;
string script = File.ReadAllText("C:\\Temp\\datascript.sql");
Microsoft.SqlServer.Management.Smo.Server server = new;
Microsoft.SqlServer.Management.Smo.Server();
server.ConnectionContext.LoginSecure = false;
server.ConnectionContext.Login = UserId.Text;
server.ConnectionContext.Password = UserPw.Text;
server.ConnectionContext.ServerInstance = SvrName.Text;
server.Databases["SYSTEM220001"].ExecuteNonQuery(script);
Errors on ExecuteNonQuery and states
server.Databases["system22root"].ExecuteNonQuery(script);
ExecuteNonQuery failed for Database 'system22root'
This code does work using exactly the same script as above
// open script file and read into script
FileInfo dbScriptFile = new FileInfo("C:\\Temp\\datascript.sql");
String script = dbScriptFile.OpenText().ReadToEnd();
// Create sql command set properties and open connection
SqlCommand cmd = new SqlCommand();
SqlConnection myConn4 = new SqlConnection(GlobalClass.gSrVrConn);
myConn4.Open();
// Parse the script. We need to remove the GO commands since
they
// are not valid T-SQL. The regular expression is necessary so
// we don't blow away valid instances of GO as part of other
words
string[] commands = Regex.Split(script, "GO *\r\n|GO\n|GO *$");
for (int i = 0; i < commands.Length; i++)
{
// If we don't have an empty command, execute it
if (!String.IsNullOrEmpty(commands[i]) && commands[i] != "\r
\n")
{
// need to put transaction in here so I can roll back
errors
// execute each command for this connection
cmd.CommandText = commands[i];
cmd.Connection = myConn4;
cmd.ExecuteNonQuery();
}
}
// close the connection
myConn4.Close();
Could someone please advise me as to why the first section of code
does not work.
Any help would be appreciated
Ian