smo

can't get EnumScript() to generate constraints

懵懂的女人 提交于 2019-12-11 11:22:34
问题 I'm trying to get programmatically what I can get manually from SSMS using Tasks > Generate Scripts The code below works fine, EXCEPT it doesn't generate any constraints. I don't get any ALTER TABLE [foo] ADD CONSTRAINT ... ON DELETE CASCADE etc etc. I've tried a lot of combinations of Dri options and on different databases as well. I'm stumped. Thanks for insight! Scripter scrp = new Scripter(srv) { Options = { ScriptDrops = false, WithDependencies = false, Indexes = true, Triggers = false,

How to get the list of SqlInstances of a particular machine

亡梦爱人 提交于 2019-12-11 06:55:33
问题 Can anyone tell me how to get remote Sqlserver instances using c# and SMO or any api? I have a remote server name "RemoteMC", which has 2 instances of sql server: "RemoteMc" and "RemoteMC\sqlexpress" I try to get the instances in code like this: Server srv=new Server("RemoteMC"); DataTable dt=SmoApplication.EnumAvailableSqlServer(true); But it returns "Host\sqlexpress" I don't know what went wrong. How can I get the result back as: RemoteMC RemoteMC\sqlexpress; ? 回答1: The SmoApplication

Using SMO, still no go… ConnectionContext.ExecuteNonQuery(script) can't understand “GO”

吃可爱长大的小学妹 提交于 2019-12-11 04:04:51
问题 SQL Server 2008 Using all the correct references I dare say: System.Data.SqlClient; Microsoft.SqlServer.Management.Smo; Microsoft.SqlServer.Management.Common; Microsoft.SqlServer.Management.Sdk.Sfc; All compiles with no errors. I have stripped code down to almost zero for easy debugging. Connecting to server alright and so on. Excuting following code: SqlConnection connection = new SqlConnection(sqlConnectionString); Server server = new Server(new ServerConnection(connection)); server

SQL SMO - checking if a user is a server admin

非 Y 不嫁゛ 提交于 2019-12-11 03:22:05
问题 I want to check if the connected user is a SQL Server admin (in the sysadmin role) and wondering if this is this is the best way to do it? private static bool IsUserAdmin(Server server) { Login login = server.Logins[server.ConnectionContext.Login]; return login.IsMember("sysadmin"); } In SQL DMO (the COM based predecessor to SMO) there was a property that hung off the SQLDMO.SQLServer object called IsServerAdmin . I'm wondering if there's something similar in SMO but that I just haven't been

Can I force SQL SMO to use READ UNCOMMITTED isolation?

谁说胖子不能爱 提交于 2019-12-11 03:02:34
问题 I have an application which is using SQL2008 SMO to grab the SpaceAvailable property of all of the databases, but it often becomes blocked by other processes for long a duration. I have noted that I can run the query it generates in SSMS with no issues in READ UNCOMMITTED isolation. So, do a way exist to force SQL SMO to use read uncommitted isolation? 回答1: Sorry for the late reply to this. Figured I'd post this in in case someone needs help in the future. You can set specify the isolation

How to create local database Instance using SMO

余生颓废 提交于 2019-12-10 21:27:56
问题 I have installed localdatabase and created a shared instance, and using smo I am able to create localdatabase. For my requirement I have to create a localdb shared instance. I am creating local database using SMO like this. ServerConnection serConn = new ServerConnection(@"(localdb)\.\sharedIns"); Server ldbServer = new Server(serConn); Database db = new Database(ldbServer , "databaseName"); db.Create(); Before creating database, I am creating shared instance through command prompt using

SQL Server SMO - Backuping - How to determine failure / success?

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:55:08
问题 If you execute backup using SMO, after successful completion, I test the SqlError for null, considering that the backup completed without errors: But, as you can see, it actually return an error of class 0 number 3014 meaning success. So the question is: Q: How can I find out if backup completed successfully or not, and how should I handle these messages and statuses cleanly? I'm afraid that there are multiple of "gotchas" here that I don't want to bite me in the ass later when this code goes

Use SMO to Change SQL Server Database Default Locations

此生再无相见时 提交于 2019-12-10 15:33:37
问题 I am using the following code to change the server database default locations (where the SQL Server keeps new database files and log files): using Microsoft.SqlServer.Management.Smo; Server smoServer = new Server(new ServerConnection(server, username, password)); server.DefaultFile = newPath; server.Alter(); server.Refresh(); // Now create a database in the new location Database smoDatabase = new Database(smoServer, database); smoDatabase.Create(); smoServer.Refresh(); Here's my problem: I

SQL Server SMO take back up locally from remote server

纵然是瞬间 提交于 2019-12-10 14:56:33
问题 My question is straight forward, how can I program SQL SMO to take local backup, by connecting to a remote server. I want to save the bak file to a local machine which is connecting to the remote server. Also I want users with only certain privilege to be able to save backups locally. 回答1: You cannot - period. Backups of SQL Server can only be saved to a local disk - local to the SQL Server itself. You cannot with any tricks or tools backup a remote SQL Server to your local harddisk. Just can

Checking for and dropping an existing table via C# and SMO

女生的网名这么多〃 提交于 2019-12-10 13:24:12
问题 I am trying to look for a SQL table by name and if it exists drop it. This all needs to be done in C# using SMO. To complicate it a bit more the table also has a schema other then "dbo". Ultimatly the table will be recreated via SMO (I have this working) but I have to make sure it is not there before I can recreate it. All of the examples that I have seen seem to be creating and then dropping the table all in the same context. In my case the table will have been created and populated in a