Well, I'm not familiar with this exact sort of code but it looks like it is saying that the sql code in the file C:\\Temp\\script.sql is causing a sql error. ExecuteNonQuery is a function that executes the sql code that it is handed as a string and returns an integer to tell you how many records (rows) were successfully acted on.
On Dec 4, 9:54 am, Ian <[email protected]> wrote: > Hi all, > > Being new to Visual Studio 2010 c#, I am trying to get to grips with > the basics. The following code > errors when running a script from sql server 2008 to create tables > against an existing database. the script does run ok if ran from, > within sql server 2008. > > The code in my c# app taken from on line examples states; > > string script = File.ReadAllText("C:\\Temp\\script.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["system22root"].ExecuteNonQuery(script); > > UserId.Text, UserPw.Text and SvrName.Text come from the user entry > form and these do work if I just try to connect to the database. > > However, when I run the above I get an error against the line > > server.Databases["system22root"].ExecuteNonQuery(script); > > Which states > > FailedOperationException was unhandled and the next line says > > ExecuteNonQuery failed for Database 'system22root > > The database system22root does exist and the script does work within > sql server 2008. > > Any help would be appreciated. > > Cheers > Ian
