SQL Server SMO/DMO ForumAny questions on SMO/DMO© 2009 Microsoft Corporation. All rights reserved.Thu, 26 Nov 2009 18:59:24 Zfec0c733-c7ac-4cde-bb42-c6a76d06ae18http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/21e3c34f-ef9e-4e2f-a2e8-804bd5065c9dhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/21e3c34f-ef9e-4e2f-a2e8-804bd5065c9dCasper Uldal Olsenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Casper%20Uldal%20OlsenGetting "wrong" data out of a microsoft.sqlserver.management.smo.database object property (using powershell 1.0)My problem:<br/>I do something like this:<br/>$dbsvr = new-object microsoft.sqlserver.management.smo.server &quot;servername&quot;<br/>$dbsvr.databases | ft name,size -autosize<br/>When i run this from two different PC's i get database sizes that are all the same (5.25 mb), the size of the first database listed - which is master.<br/>I started to try out a few things, and found out that when i run the same command from one of our servers it returns the correct sizes...<br/><br/>Anyone have any idea if this a SMO issue, or maybe some package software version issue...- or what?<br/><br/><br/>Thu, 26 Nov 2009 13:50:29 Z2009-11-26T18:59:24Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e77bcdb0-d0d2-4f57-a1ce-642b686c8530http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e77bcdb0-d0d2-4f57-a1ce-642b686c8530EternalCoderhttp://social.msdn.microsoft.com/Profile/en-US/?user=EternalCoderSQL Server 2005 and 2008 SMO compatibilty issues<p align=left><span>I have SMO library references in an application which needs to be able to compile and run on both SQL Server 2005 and SQL Server 2008. How do I make sure my application is compatible with both? </span></p> <p align=left><span></span> </p> <p align=left><span>I developed on my application with SQL Server 2005.When I add SMO library references to my project they show up as:</p> <p align=left>  &lt;Reference Include=&quot;Microsoft.SqlServer.ConnectionInfo, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL&quot; /&gt;<br>    &lt;Reference Include=&quot;Microsoft.SqlServer.Smo, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL&quot; /&gt;</p> <p align=left> </p> <p align=left>But obviously this fails to compile if the server has only SQL server 2008 installed. Is there a work around for this? I tried adding the libraries with specific version set to false, this compiles fine but fails when the application is run even with SQL Server 2005.</p> <p align=left><font face=Arial size=2></font> </p> <p align=left>Also, in another solution mentioned in this forum: &quot;If you recompile your application with SQL Server 2008 SMO then it will work with both SQL Server 2005 and 2008.&quot; - </p> <p align=left>If I understand this correctly, this means recompile with version 10 of the smo libraries? Won't this be an issue if one tries to compile on SQL server 2005 then?</p> <p> </p> <p align=left>Note that we are not shipping smo libraries with the application, rather the application either runs on the server which has SQL server 2005 or 2008 installed or would try to connect to another server with SQL server 2005/2008 running.</span></p>Thu, 09 Oct 2008 17:28:32 Z2009-11-25T19:36:39Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/f54b87e3-b12e-43f9-a776-ef1bccea5420http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/f54b87e3-b12e-43f9-a776-ef1bccea5420MarceloRamoshttp://social.msdn.microsoft.com/Profile/en-US/?user=MarceloRamosProblems with SMO 2008 Redist + Microsoft.SqlServer.BatchParserClient<p align=left><font face=Arial size=2></font> </p> <p>Hi,</p> <p align=left> </p> <p align=left> I'm trying to run my application using SMO 2008. Everything works just fine if i have sql 2008 server installed.</p> <p align=left> </p> <p align=left>My problem starts when I try to use SMO 2008 Redist. First it does not install Microsoft.SqlServer.BatchParserClient or Microsoft.SqlServer.BatchParser. After a little research, I found that this is a problem and that these files have to be copied manually to the folder where the application runs.</p> <p align=left> So I tried copying the files: No luck, i get a Could not load file (Below).</p> <p align=left> Then i tried to adding these files to the GAC manually: Same thing, Could not load file:</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>Microsoft.SqlServer.Management.Smo.FailedOperationException: ExecuteNonQuery failed for Database 'DB2008'.  ---&gt; System.IO.FileLoadException: Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)</p> <p align=left> </p></div></div> <p align=left> </p> <p></p> <p align=left> </p> <p align=left> Then I looked at the version of the files and they seemed to be version 10.0.144.32, so I went to my app.config and added a bind redirection from 10.0.0.0 to 10.0.144.32. now it finds the assembly but says that Manifest doesnt mach the reference:</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div>Microsoft.SqlServer.Management.Smo.FailedOperationException: ExecuteNonQuery failed for Database 'DB2008'.  ---&gt; System.IO.FileLoadException: Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=10.0.1442.32, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)<br>File name: 'Microsoft.SqlServer.BatchParser, Version=10.0.1442.32, Culture=neutral, PublicKeyToken=89845dcd8080cc91' ---&gt; System.IO.FileLoadException: Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)<br>File name: 'Microsoft.SqlServer.BatchParser, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'<br> <p align=left> </p></div></div> <p align=left> </p> <p></p> <p align=left>Then I checked all the 3 versions: Microsoft.SqlServer.SMO, Microsoft.SqlServer.BatchParser and Microsoft.SqlServer.BatchParserClient. The three are 10.0.1442.32 which confused me even more because why it would need a binding redirection to load the file?</p> <p align=left> </p> <p align=left>Anyways, I'm not sure what to do to make this app work on a machine with SMO 2008 Redist only. </p> <p align=left> </p> <p align=left>Did anyone had same/similar problem and  know a work around for this?<br></p> <p align=left>Thanks,</p> <p align=left>Marcelo</p>Wed, 30 Jul 2008 19:52:43 Z2009-11-25T19:05:27Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/15de3c55-6c64-478f-9184-9faeb8133102http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/15de3c55-6c64-478f-9184-9faeb8133102MikeC01http://social.msdn.microsoft.com/Profile/en-US/?user=MikeC01SMO server.databases properties - IndexSpaceUsage (permissions)According to msdn:  <span>To get <strong>Database</strong> object properties, users can be a member of the <strong>public</strong> fixed server role.<br/> <br/> If I impersonate a login with only the public server role, I can get properties like &quot;Name&quot;, but I cannot get properties like &quot;DataSpaceUsage&quot;.  If I switch to a user with sysadmin role, I can get DataSpaceUsage.<br/> <br/> That makes me think it is a permissions issue.  Or, a setting on the server that hides these properties.  Or it is my code.<br/> <br/> code:<br/> # Load SMO extension<br/> [System.Reflection.Assembly]::LoadWithPartialName(&quot;Microsoft.SqlServer.Smo&quot;) | Out-Null;<br/> <br/> $sc2 = New-Object ('Microsoft.SqlServer.Management.Common.ServerConnection')<br/> $sc2.ConnectAsUser = $true<br/> $sc2.ConnectAsUsername = '&lt;login&gt;@&lt;domain&gt;'<br/> $sc2.ConnectAsUserPassword = '&lt;password&gt;'<br/> $sc2.ServerInstance = '&lt;server&gt;'<br/> <br/> $sqlservers2 = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $sc2<br/> <br/> $sqlservers2.databases[0].name (this returns the name)<br/> $sqlservers2.databases[0].DataSpaceUsage (this returns nothing)<br/> <br/> -Thanks <br/> <br/> Mike</span>Mon, 23 Nov 2009 20:31:04 Z2009-11-24T21:47:12Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/319e916c-0656-4e08-b7cc-98915e1556d4http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/319e916c-0656-4e08-b7cc-98915e1556d4Jellis3dhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jellis3dSystem.Data.SqlClient.SqlError: There is insufficient free space on disk volume 'C:\' to create the database.I received the following error...<br/><br/>*****<br/>System.Data.SqlClient.SqlError: There is insufficient free space on disk volume 'C:\' to create the database. The database requires 2739929088 additional free bytes, while only 801185792 bytes are available. (Microsoft.SqlServer.Smo)<br/>*****<br/><br/>I have looked at the solution recommended on this forum <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=79848">http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=79848</a> but I am still baffled as to why it says that I don't have enough disk space?  I ran the RESTORE FILELISTONLY command and it told me that the &quot;size&quot; was 1.4 GB and I've got 60GB left. I am starting to resolve that my .BAK is corrupt.  Are there any other reasons as to why I am unable to restore my backup?<br/><br/>Thanks for any help in advance!Thu, 19 Nov 2009 15:09:53 Z2009-11-24T11:03:27Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3f0fe713-5ad9-4fd8-8d37-1fd571b9c048http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3f0fe713-5ad9-4fd8-8d37-1fd571b9c048jack_minghuihttp://social.msdn.microsoft.com/Profile/en-US/?user=jack_minghuipowershell in sqlserverI had installed powershell in sqlserver 2005 server.And I also had enabled cmdshell in sqlserver .<br/>While I execute the powershell script in sqlserver, Iencounter following error:<br/><strong>'PowerShell' is not recognized as an internal or external command,operable program or batch file.<br/></strong><br/>Does anyboby has any idea about this issue?<hr class="sig">happyManMon, 23 Nov 2009 09:02:12 Z2009-11-23T09:02:14Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/2c4669af-1899-48d4-bb04-4a27fdc3b0cahttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/2c4669af-1899-48d4-bb04-4a27fdc3b0caKJRBhttp://social.msdn.microsoft.com/Profile/en-US/?user=KJRBSMO generated Triggers and the placement of sp_settriggerorderI generated a trigger with SMO and set trigger order using this syntax:<br/> <div style="color:#008000;font-size:x-small">o.DeleteOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last;<br/>o.InsertOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last;<br/>o.UpdateOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last;<br/><br/></div> The resulting trigger looks like this:<br/><br/> <pre lang=x-sql>CREATE TRIGGER [dbo].[test_trigger] ON [dbo].[Table_1] AFTER INSERT, DELETE, UPDATE AS if 1= 1 begin insert into autit_t (dt) values (getdate()) end EXEC sp_settriggerorder @triggername=N'[dbo].[test_trigger]', @order=N'Last', @stmttype=N'DELETE' EXEC sp_settriggerorder @triggername=N'[dbo].[test_trigger]', @order=N'Last', @stmttype=N'INSERT' EXEC sp_settriggerorder @triggername=N'[dbo].[test_trigger]', @order=N'Last', @stmttype=N'UPDATE'</pre> When I use SQL Servere Management Studio to Modify or Script the trigger  <strong>EXEC sp_settriggerorder</strong> are always scripted as a part of create or alter script. <br/><br/>My question is if this looks ok. <em><strong>Should there be a &quot;GO&quot; statement before the trigger body and  those sp_settriggerorder statements. <br/>If there should be a GO statement. Should it be generated by SMO or should I put &quot;GO&quot; in trigger body text?</strong></em><br/><br/>The code to gnerate triggers looks like this:<br/> <pre lang="x-c#">Server dbServer = new Server(&quot;xxx\\MSSQL2008,49999&quot;); var selectedDatabase = dbServer.Databases[&quot;TestTest&quot;]; selectedDatabase.DatabaseOptions.AutoClose = true; Table tb = selectedDatabase.Tables[&quot;Table_1&quot;]; Trigger tr = new Trigger(tb, &quot;test_trigger&quot;); tr.TextMode = false; tr.Insert = true; tr.Update = true; tr.Delete = true; tr.DeleteOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last; tr.InsertOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last; tr.UpdateOrder = Microsoft.SqlServer.Management.Smo.Agent.ActivationOrder.Last; tr.TextBody = @&quot;if 1= 1 begin insert into autit_t (dt) values (getdate()) end&quot;; dbServer.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql; tr.Create(); var commands = dbServer.ConnectionContext.CapturedSql.Text; var builder = new StringBuilder(); foreach (var s in commands) { Console.WriteLine(s); }</pre> <br/><br/>Mon, 23 Nov 2009 05:14:28 Z2009-11-23T05:14:28Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/50949b00-fc1c-4fed-9912-6b710a727904http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/50949b00-fc1c-4fed-9912-6b710a727904meetc1982http://social.msdn.microsoft.com/Profile/en-US/?user=meetc1982How to set DefaultConstraint of column using SMO??<p>How to set DefaultConstraint of column using SMO??<br/><br/>Plz help.</p>Tue, 17 Nov 2009 21:30:40 Z2009-11-19T21:52:24Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/112949c6-345b-43ad-baca-4f1a556a1387http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/112949c6-345b-43ad-baca-4f1a556a1387Angel SQLhttp://social.msdn.microsoft.com/Profile/en-US/?user=Angel%20SQLSMO usage<p>Would someone please tell me why do i use SMO and where?</p> <br/> <p>I know very well how to use this but dont know in which condition i exactly use this or not use this.</p> <br/> <p>Thanks</p>Fri, 13 Nov 2009 04:50:39 Z2009-11-23T09:55:31Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/94815538-2197-4e45-b1e4-25a085506c9fhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/94815538-2197-4e45-b1e4-25a085506c9fCatchmesivahttp://social.msdn.microsoft.com/Profile/en-US/?user=CatchmesivaHow to Take the backup for a chat browser using vb or sqlI bought a chat browser and i am using that now. I would like to know how to get the backup for the chat browser, if my system crashes or that chat engine crashes. i want to have a permanent backup whatever i am chatting to another person. i dont believe with the default browser back up and want to create a new application or a program to have a backup permanently in another computer (like server). How could i do that with VB or sql or oracle(which would be the backend).Thu, 19 Nov 2009 05:16:55 Z2009-11-19T05:16:56Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/095bcc97-72b1-43d5-95f4-a2650bf60f19http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/095bcc97-72b1-43d5-95f4-a2650bf60f19Ventsislav Velevhttp://social.msdn.microsoft.com/Profile/en-US/?user=Ventsislav%20VelevHow to determine a partial vs full backup set?I've written application that uses SMO to backup / restore database. I largely mimic the interface found in SQL Server Management Studio 05. <br/> In the restore display I display all available backup sets and I query msdb to get the info for each backup set via the following query:<br/> <br/> &quot;select backup_set_id,name,description,recovery_model,type,server_name,database_name,position,<br/>  backup_finish_date,backup_size,user_name,expiration_date<br/>  from msdb.dbo.backupset Where msdb.dbo.backupset.database_name = '&quot; + dbName.Text + &quot;'&quot;;<br/> <br/> How do I determine if the backup was full or incremental, so I can set the Partial property??Fri, 13 Nov 2009 18:14:34 Z2009-11-17T16:40:40Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/68b44da1-cfaa-4f03-91f3-1e2c7f44d9eahttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/68b44da1-cfaa-4f03-91f3-1e2c7f44d9eaDaleTurleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=DaleTurleyCopy an SMO.Table object?Hi All,<br/><br/>Does anybody know how to create a copy of a Table objects using SMO??<br/><br/>I need to create a Table object that references a table in a database. Then copy that Table Object to a new object, rename the old table and the create the new table effectivly backing it up???<br/><br/>I can't seem to find a way of doing this :(Tue, 10 Nov 2009 14:39:46 Z2009-11-16T12:37:48Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e707af3e-92c8-46d0-9c4b-a77f0d71efb1http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e707af3e-92c8-46d0-9c4b-a77f0d71efb1Caroline81http://social.msdn.microsoft.com/Profile/en-US/?user=Caroline81List all SQL servers on networkHello,<br/> <br/> I'm trying to find a way to produce a list of all the SQL servers running on a network and have been looking at the SQLDataSourceEnumerator GetDataSources method. However there is a note saying this may not show all the SQL servers and may return a different list each time it is called. I would be grateful if anyone has anymore information on why this will not always return a complete list and may be different each time or if anyone knows if there is a way I can get a list of SQL servers that is more consistent?<br/> <br/> <br/> Thanks,<br/> <br/> Caroline<br/> <br/>Fri, 13 Nov 2009 10:39:23 Z2009-11-23T09:56:14Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/b2a370f5-6d6e-4c96-907e-51d20f95996ehttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/b2a370f5-6d6e-4c96-907e-51d20f95996eAndrew Butenkohttp://social.msdn.microsoft.com/Profile/en-US/?user=Andrew%20Butenkohow to convert Microsoft.SqlServer.Management.Smo.DataType to System.Type<p>Hi there, I would like to map correctly SQL types (Microsoft.SqlServer.Management.Smo.DataType and System.Data.SqlDbType) to CLR types.</p> <p>Is it possible to not use user function like this:</p><font size=2> <p></font><font size=2>System.</font><font color="#008080" size=2>Type</font><font size=2> GetSystemTypeBySqlType(</font><font size=2><font size=2>Microsoft.SqlServer.Management.Smo.</font><font color="#008080" size=2>DataType </font></font><font size=2>typeSql)</p> <p>{</p> <p></font><font color="#0000ff" size=2>switch</font><font size=2> (typeSql.Name)</p> <p>...</p></font><font size=2> <p>}</p> <p> </p> <p>It is strange that <font color="#008080" size=2>StoredProcedureParameter</font><font size=2>.<font size=2>DataType has property SqlDataType and does not have SystemType. It would be positive to map this type automatically, because new types appear sometimes :)</p></font></font></font>Thu, 05 Oct 2006 11:53:24 Z2009-11-16T07:01:46Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3f0c262a-9e33-444f-bf4b-7d411305e139http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3f0c262a-9e33-444f-bf4b-7d411305e139RicardodeAlmeida27http://social.msdn.microsoft.com/Profile/en-US/?user=RicardodeAlmeida27Add a New Column with SMO not null I'm making a Utility that compares 2 Databases and makes one Equal to the other by changing and adding objects (Tables, Views, UserDefinedDatatypes, Defaults, Functions etc etc etc)<br/><br/>I'm trying to add a Column, not null  with a userdefineddatatype wich has a Default defined, the problem is this:<br/>I'm not able to use BindDefault before the column is added, because it does not exist (ofcourse) and I'm not able to add the column to the table because it has data and says it has to have a default defined.<br/><br/>Another thing, when I atribute the DataType I thought it might also Bind the Default (because it is Binded to the UserDefinedDataType) but it also didn't.<br/><br/>Hope I made myself clear.<br/><br/>Tks in advance.<br/><br/>PS - I'm solving this by making Nullable=True<br/>Update the new collumn with the defaul value<br/>BindDefault and Nullable=False<br/><br/>But this not a good sollution, I wanted to make changes to the table for several columns and only make the Alter of the Table at the End.<br/>Wed, 11 Nov 2009 13:24:40 Z2009-11-19T09:43:18Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/0c2f432e-5ac1-4037-92a4-f539c717d21dhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/0c2f432e-5ac1-4037-92a4-f539c717d21dAmos Somahttp://social.msdn.microsoft.com/Profile/en-US/?user=Amos%20SomaError Dropping Database using SMO<p>I use the method shown below to drop a SQL Server database using SMO. On occasion, I'll get the following error message when this method is called. I'm not sure what I can do to prevent an error like this from happening. I don't think I have any control over it. The error message is this:</p> <p> </p> <p><span style="font-size:12pt;font-family:'Times New Roman','serif'"><em>Drop all active database connections failed for Server 'SDTPLCHost1\SQLServer2005'. : An exception occurred while executing a Transact-SQL statement or batch.: Process ID 57 is not an active process ID.</em></span></p> <p><span style="font-size:12pt;font-family:'Times New Roman','serif'"><em></em></span> </p> <p><span style="font-size:12pt;font-family:'Times New Roman','serif'">Here is the method I wrote that uses SMO to drop a database:</span></p> <p><span style="font-size:12pt;font-family:'Times New Roman','serif'"></span> </p><span style="font-size:12pt;font-family:'Times New Roman','serif'"><font size=2> <p></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> DropDatabase(</font><font color="#0000ff" size=2>string</font><font size=2> serverName, </font><font color="#0000ff" size=2>string</font><font size=2> databaseName)</p> <p>{</p> <p></font><font color="#008080" size=2>   Server</font><font size=2> smoServer;</p> <p></font><font color="#0000ff" size=2>   try</p></font><font size=2> <p>   {</p></font><font size=2> <p>      smoServer = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>Server</font><font size=2>(serverName);</p></font> <p><font size=2> </p> <p></font><font color="#0000ff" size=2>      if</font><font size=2> (!smoServer.Databases.Contains(databaseName))</p> <p></font><font color="#0000ff" size=2>         return</font><font size=2>;</font></p> <p><font size=2> </p></font><font size=2> <p>      smoServer.KillAllProcesses(databaseName);</p> <p>      smoServer.KillDatabase(databaseName);</p> <p>   }</p> <p></font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>Exception</font><font size=2> ex)</p> <p>   {</p> <p></font><font color="#0000ff" size=2>      throw</font><font size=2> </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>Exception</font><font size=2>(BuildExceptionMessage(ex));</p> <p>   }</p> <p>}</p></font></span>Wed, 28 Mar 2007 13:07:36 Z2009-11-11T14:13:17Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/934597ff-e1a1-4bff-8c4e-3ad66e744378http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/934597ff-e1a1-4bff-8c4e-3ad66e744378SPCShttp://social.msdn.microsoft.com/Profile/en-US/?user=SPCSSmo Restore.ReadFileList(server) fails <p>I am writing a C# application to backup and restore a SQL Server database from one instance to another. I am trying to get the logical names of the data and log files of the database from the .bak file and it fails at the statement &quot;DataTable dt = restore.ReadFileList(server);&quot; with the error {&quot;Incorrect syntax near the keyword 'WITH'.\r\nIncorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.&quot;}<br/><br/>Is there anything I have to do before I can use the restore.ReadFileList method ? The logical names of the database files have '.' and '_' in the names, &quot;DBName_data.mdf&quot; and &quot;DBName_log.ldf&quot; . Could it be the reason for this error ?<br/><br/>I tried restore from SQL Server Express Instance and SQL Server Full Version to SQL Server Full Version. Both of the scenarios resulted in the same error and Restore could not complete.</p>Thu, 05 Nov 2009 21:27:15 Z2009-11-10T22:19:09Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/c719e624-1a44-43bd-876d-6c7e09382c49http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/c719e624-1a44-43bd-876d-6c7e09382c49AndyB1978http://social.msdn.microsoft.com/Profile/en-US/?user=AndyB1978SQL Backup ContentsHi,<br/><br/>I'm just wondering about the actual contents of a full database backup within SQL 2005.<br/><br/>If I start a full backup at 01:00 in the evening, and the backup takes 1 hour to run, what will be the contents of the backup?<br/><br/>If I perform a restore from the backup, up to what point will the data be correct?  Will it recover up until the start of the backup, and then require transaction logs applying to bring it up to 02:00?  Or, alternatively will the backup contain all data up until 02:00 so no additional recovery is required?  (If we assume that I do not need any data after the end of the backup)<br/><br/>Or is there a third option which I've missed?<br/><br/>Thanks<br/>Mon, 09 Nov 2009 12:39:28 Z2009-11-16T10:06:08Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/85234788-a9a6-40ae-baea-9d234b7a9ef6http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/85234788-a9a6-40ae-baea-9d234b7a9ef6Calinoiu Alexandruhttp://social.msdn.microsoft.com/Profile/en-US/?user=Calinoiu%20AlexandruStoredProcedureParameter commentHi, <div><br/></div> <div> I am currently creating a StoredProcedure using this kind of code:</div> <div><br/></div> <div> <pre lang="x-c#">StoredProcedure storedProcedure = new StoredProcedure(database, name); StoredProcedureParameter storedProcedureParameter = new StoredProcedureParameter(storedProcedure, parameterName, t.VariantType); storedProcedure.Parameters.Add(storedProcedureParameter);</pre> This will generate sql like the folowing:</div> <div><br/></div> <div> <pre lang=x-sql>SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO ALTER PROCEDURE [dbo].[Book10] @Product [int] AS</pre> I want to be able to add comments to the sql parameter, so that the sql code should look like:</div> <div> <pre>SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO ALTER PROCEDURE [dbo].[Book10] @Product [int] /*comment for firt param */ AS</pre> Is there a way to achive this using StoredProcedure and StoredProcedureParameter ?</div> <div><br/></div> <div>Thank you,</div><hr class="sig">&quot;ME: How do I achive the imposible !&quot; &quot;The Wise Man: With entusiams !&quot;Fri, 06 Nov 2009 07:05:24 Z2009-11-09T06:49:47Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/1bef2612-6820-4ddd-bb64-087d0bc9d93dhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/1bef2612-6820-4ddd-bb64-087d0bc9d93dGRKhttp://social.msdn.microsoft.com/Profile/en-US/?user=GRKBackup and recovery of SQL Server using VB.net<p>Hi,</p> <p>   I have  a small application in which i'm using Sql Server as Database. my requirement is how to take the backup of the entire database or some tables from the database when there is any delete from the database. My requirement is to do from the VB.net application.Hope i delivered my question correctly. Any little help is beneficial to me.</p> <p>-regards</p> <p>GRK</p>Tue, 05 Dec 2006 04:55:38 Z2009-11-07T10:26:03Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e758e143-fc0d-4a6f-bf3a-f8a847065b9ehttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/e758e143-fc0d-4a6f-bf3a-f8a847065b9eGurmit Teotiahttp://social.msdn.microsoft.com/Profile/en-US/?user=Gurmit%20Teotiahow to log execution result of scriptHello All ,<br/><br/>I'm using following code to execute a .sql script-<br/><br/>SqlConnection conn = new SqlConnection(sqlConnectionString);<br/>Server server = new Server(new ServerConnection(conn));<br/>server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(ConnectionContext_InfoMessage);<br/>server.ConnectionContext.ExecuteNonQuery(script, ExecutionTypes.ContinueOnError);<br/><br/><br/>This script file has bunch of &quot;ALTER&quot; statement which apply primary key and foreign key constraint. I'd like to log the execution output of that script to a text file. Please advise how that can be achieved.<br/><br/>Regards,<br/>Gurmit<br/><br/>Tue, 20 Oct 2009 13:07:44 Z2009-11-06T21:56:43Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/a1df7a28-a193-463e-984a-59402d13262ahttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/a1df7a28-a193-463e-984a-59402d13262aT2 Rockshttp://social.msdn.microsoft.com/Profile/en-US/?user=T2%20RocksProperty LoginSecure cannot be changed or read after a connection string has been set.hi,<br>      i am trying to copy a table from one database to another using SMO's Transfer. I am getting the following error when i execute the Transfer.TransferData()<br><br>&quot;Property LoginSecure cannot be changed or read after a connection string has been set.&quot;<br>i am using .net 2.0(x64),sql 2005 std edition<br><br>please help....................Fri, 06 Mar 2009 08:36:53 Z2009-11-04T22:41:44Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fe8dd4c4-3550-48cd-baaf-fae9246ff87ahttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fe8dd4c4-3550-48cd-baaf-fae9246ff87arsj2http://social.msdn.microsoft.com/Profile/en-US/?user=rsj2SMO Restore unable to complete restore<p>I am trying to write an application that will backup and restore a SQL Server Express 2008 database.  I am using SMO to do this task.  The backup seems to work fine, but when I try and restore from it using the SMO Restore oblect the restore seems to complete, but really does not.  By this I mean the completed event fires properly and no visible exceptions have been thrown.  When I open the SQL Server Management Studio, the database state shows as &quot;Restoring...&quot; and the database itself is unavailable.  If I try to bring the database online programmatically following the restore, an exception is thrown indicating that &quot;ALTER DATABASE is not permitted while a database is in the Restoring state ALTER DATABASE statement failed&quot;.  I am using Microsoft Visual Studio 2008 SP1 and Microsoft SQL Server Express 2008 for this project.  The following is the code that I am using to try and do this:<br/>  <br/>        public  bool RestoreSMO(string filename)<br/>        {<br/>            try<br/>            {<br/>                Microsoft.SqlServer.Management.Common.ServerConnection conn = <br/>                    new Microsoft.SqlServer.Management.Common.ServerConnection();<br/>                conn.ServerInstance = this.DatabaseName;<br/>                conn.LoginSecure = false;<br/>                conn.Login = this.UserName;<br/>                conn.Password = this.Password;<br/>                conn.DatabaseName = &quot;master&quot;; // You cannot restore a database that you are connected to<br/>                <br/>                <br/>                Microsoft.SqlServer.Management.Smo.Server svr = new Server(conn);</p> <p>                svr.LoginMode = ServerLoginMode.Mixed;<br/>                <br/>                Database currentDb = svr.Databases[this.Database];<br/>                if (currentDb != null)<br/>                {<br/>                    svr.KillAllProcesses(this.Database);<br/>                }<br/>                Restore restoreDB = new Restore();<br/>                restoreDB.Database = this.Database;<br/>                /* Specify whether you want to restore database, files or log */<br/>                restoreDB.Action = RestoreActionType.Database;<br/>                restoreDB.Restart = true;<br/>                <br/>                restoreDB.Devices.AddDevice(filename, DeviceType.File);</p> <p>                restoreDB.ReplaceDatabase = true;<br/><br/>                restoreDB.NoRecovery = true;</p> <p>                // * Wiring up events for progress monitoring <br/>                restoreDB.PercentComplete += new PercentCompleteEventHandler(restoreDB_PercentComplete);<br/>                restoreDB.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(restoreDB_Complete);<br/>                <br/>                restoreDB.SqlRestore(svr);<br/>                <br/>                return true;<br/>            }<br/>            catch (Exception ex)<br/>            {<br/>                Debug.WriteLine(ex.Message);<br/>                throw ex;<br/>            }</p> <p>        }</p>Tue, 03 Nov 2009 16:09:00 Z2009-11-10T11:42:58Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3bd2f286-c2a4-41f3-ba17-304d9ca719f4http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/3bd2f286-c2a4-41f3-ba17-304d9ca719f4Alfred Zwiephttp://social.msdn.microsoft.com/Profile/en-US/?user=Alfred%20ZwiepFailed to connect to server<p align=left>Using VB.Net 2005, Standard edition and the SQL-Server Express-edition. </p> <p align=left> </p> <p align=left>Question: I'd like to write a backup-system in my small app. Hereby I need to detach the database, before copying the files. </p> <p align=left> </p> <p align=left>References added:</p> <p align=left>Microsoft.SqlServer.ConnectionInfo</p> <p align=left>Microsoft.SqlServer.Smo</p> <p align=left>Microsoft.SqlServer.SmoEnum</p> <p align=left>Microsoft.SqlServer.SqlEnum</p> <p align=left> </p> <p align=left>Source code:</p> <p align=left><font color="#0000ff" size=2>Dim</font><font size=2> srv </font><font color="#0000ff" size=2>As</font><font size=2> </font><font color="#0000ff" size=2>New</font><font size=2> Server</p> <p>srv.DetachDatabase(</font><font color="#800000" size=2>&quot;DBname&quot;</font><font size=2>, </font><font color="#0000ff" size=2>True</font><font size=2>)</p> <p></p> <p> </p> <p align=left>Problem: Inner exception error: Failed to connect to server. </p> <p align=left> </p> <p align=left>I cannot see why connecting to the server is a problem. Using tables-adapters, etc. are no problem at all. I'm stuck; could anyone point me in the right direction ?</p> <p align=left> </p> <p align=left>Thanks in advance; greetings from Holland, </p> <p align=left> </p> <p align=left> </p> <p align=left>Alfred Zwiep</p> <p align=left> </p> <p align=left></font> </p>Thu, 17 Apr 2008 18:00:07 Z2009-11-04T13:19:19Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/7690214d-4cba-4757-9956-05f0ca408db5http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/7690214d-4cba-4757-9956-05f0ca408db5sohail Khaliqhttp://social.msdn.microsoft.com/Profile/en-US/?user=sohail%20Khaliqdatabase backup programmatically<p align=left><font face=Arial size=2><span> </p> <p align=left><font face=Arial size=2>Hello all !</font></p> <p>I am Delveloping Visual C# 2005 windows application which is connected to SQL server 2000 database at the back end.and my task is to create a button and when i press that button it should create the database backup automatically.</p> <p align=left>Can anybody Help me</p> <p align=left>thanks</p> <p align=left><br></span></font> </p>Tue, 06 May 2008 16:16:19 Z2009-11-04T12:34:25Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/99596b45-2813-4803-92bb-3f4c117abb26http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/99596b45-2813-4803-92bb-3f4c117abb26jwbutler123http://social.msdn.microsoft.com/Profile/en-US/?user=jwbutler123Error produced when executing database.truncatelogI recently upgraded from sql 2005 to sql 2008.  I had a routine to truncate the log using the database.truncatelog method.  But now when I run the following code I get the following error.  Books on line does not indicate that the method should not be used on 2008 databases.  Note:  The database was detched from a 2005 sql instance and attached to a sql 2008 instance.  Any ideas?<br/> <pre lang=x-vbnet>db.Shrink(0, ShrinkMethod.Default) db.TruncateLog() </pre> <p>Error:</p> <span lang=EN> <p>Microsoft.SqlServer.Management.Smo.UnsupportedVersionException: This method or property is accessible only while working against a version earlier than SQL Server 2008.</p> </span> <p>Thanks,<br/>John</p>Mon, 26 Oct 2009 15:34:16 Z2009-10-29T20:32:04Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/f268d48e-34eb-452b-9565-e6e3e4a3c2c4http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/f268d48e-34eb-452b-9565-e6e3e4a3c2c4Delinghttp://social.msdn.microsoft.com/Profile/en-US/?user=DelingHow to create a Table with a composite primary key with Microsoft.SqlServer.Management.Smo objects?I am trying to create a table in my C# program with Microsoft.SqlServer.Management.Smo.Table. This table would have 3 columns consisting the PK. However, Microsoft.SqlServer.Management.Smo.Column doesn't seem to allow to specify that. Any ideas? Thank you.Tue, 27 Oct 2009 19:44:06 Z2009-11-03T11:27:28Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/dc9b1f00-41b4-4fff-8970-1a0c9331bd0ehttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/dc9b1f00-41b4-4fff-8970-1a0c9331bd0eMy_Name_Is_Fredhttp://social.msdn.microsoft.com/Profile/en-US/?user=My_Name_Is_Fredattempting to restore to a new DB and it keeps trying to save the mdf file as the original mdf file despite using the RelocateFiles stuffThis is using SQL Server 2005.<br/> <br/> I've had several problems and this is where I've ended up.<br/> <br/> 1. Despite the claim that this will create a new DB it doesn't.  I was getting errors about the new DB not having the specified logical file name (despite the new DB not existing).  At the time I was specifying the logical file name as it appeared in the backup.<br/> 2. if I manually create the DB I still get the error as above unless I manually change the logical file name via sql management studio.  This is meant to be an automated solution so that is not accceptable.<br/> 3. I am now giving it the logical file name of the DB that I want to restore onto (if it doesn't exist I create it), but now it's trying to save the file with the old physical file name, which is absolutely *not* what I want.<br/> <br/> My instincts tell me I need to change the logical file name in the database I'm wanting to restore onto, but I don't know how to do this via SMO.  All of the explanations I've seen are through the SQL Management Studio and that is not acceptable as this needs to be an automated solution.<br/> <br/> I have copy/pasted the code from the msdn examples verbatim changing only the dbName and the .bak file location and it has consistently failed.<br/> <br/> Here is the code in it's current incarnation.<br/> <br/> <pre>var serverName = @&quot;myServer&quot;; var dbName = &quot;MIQDesignTest3&quot;; var backupFileName = @&quot;D:\myDirectory\MIQDesignTest.bak&quot;; var server = new Server(serverName); var createDB = !server.Databases.Contains(dbName); if (createDB) { var newDB = new Database(server, dbName); newDB.Create(); } var bakDevice = new BackupDeviceItem(backupFileName, DeviceType.File); var restore = new Restore(); restore.Database = dbName; restore.NoRecovery = true; restore.Action = RestoreActionType.Database; restore.Devices.Add(bakDevice); restore.ReplaceDatabase = true<br/> <br/>  var mdf = new RelocateFile(); var ldf = new RelocateFile(); mdf.LogicalFileName = dbName; mdf.PhysicalFileName = String.Format(@&quot;{0}\{1}.mdf&quot;, server.Information.MasterDBPath, dbName); ldf.LogicalFileName = dbName + &quot;_log&quot;; ldf.PhysicalFileName = String.Format(@&quot;{0}\{1}.ldf&quot;, server.Information.MasterDBPath, dbName); Console.WriteLine(mdf.PhysicalFileName); Console.WriteLine(ldf.PhysicalFileName); restore.RelocateFiles.Add(mdf); restore.RelocateFiles.Add(ldf); restore.SqlRestore(server);</pre> <br/> <br/> <br/> <br/> <br/> <br/> What do I need to do to make this work?Mon, 26 Oct 2009 21:05:33 Z2009-11-05T07:04:27Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fd31793c-2757-4cef-84af-36906d309548http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fd31793c-2757-4cef-84af-36906d309548ramkannahttp://social.msdn.microsoft.com/Profile/en-US/?user=ramkannaDatabase Mail SQL 2005<p><span>I'm having a problem with database mail in SQL Server 2005.  I have set up a default profile. My SMTP server is not a localhost instead i've fed SMTP ip address and port no. The same setting in Outlook express works fine for Incoming and outgoing mails. but in SQL 2005 i'm not able to send test emails. </span><span>The following errors show up in the sql server agent log </span></p> <p><span>Message<br>The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2006-06-13T10:47:27). Exception Message: Could not connect to mail server. (An established connection was aborted by the software in your host machine).<br>)</span></p> <p><span>Please help me out....</span></p> <p><span> </p> <p><br><br></p></span>Tue, 13 Jun 2006 05:36:18 Z2009-10-27T06:35:25Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/479651b7-e838-4d4d-b298-02cf791ced57http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/479651b7-e838-4d4d-b298-02cf791ced57cakirhalhttp://social.msdn.microsoft.com/Profile/en-US/?user=cakirhalThe Integration Services<font size=2><span style="font-family:Arial">Hi,<br><br>I am developing a program that gets all object from a SQL Server 2000 database so as to copy all objects including tables, storedprocs etc to another database. However something gone wrong with this line of code:<br><br>using Microsoft.SqlServer.Management.Smo;<br>using Microsoft.SqlServer.Management.Common;<br><br>Transfer t = new Transfer(db);<br><br>TransferData();<br><br>It throws that error while executing:<br><br>The Integration Services component is not installed or you do not have permission to use it.<br><br>SQL Server 2000 Enterprise and SQL Server 2005 Express Editions are installed on my machine. I searched a solution for this problem but I found</span></font><font size=2><span style="font-family:Arial"> nothing</span></font><font size=2><span style="font-family:Arial">. How can I install SSIS on my machine or anybody have any solution for this problem.<br><br></span></font>Fri, 15 Aug 2008 15:39:36 Z2009-10-26T15:43:00Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/40d3343a-e989-4b6b-a224-41e37386d0c7http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/40d3343a-e989-4b6b-a224-41e37386d0c7David Harrishttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20HarrisEnumAvailableSqlServers() returns nothing when there is no network connection<p>I am having a weird issue. I am using SMO's <span class=forumName id="_ctl0_MainContent_PostFlatView__ctl0_PostSubject">EnumAvailableSqlServers() </span>method to fill a drop-down box. If I am connected to a network, it shows all networked Sql Servers, including the local instance. When I am not connected to any network, it shows nothing. Even if I connect to an ad-hoc wireless network, it works correctly, showing only the local instance, but if the wireless card is removed (and no other networks are enabled) it is blank. SQL Server Browser is running. I am running both SQL Server and SQL Browser services under a local administrator account (for testing). </p> <p> </p> <p>The problem is that these are handheld devices and sometimes they will be on a network, and other times they will be inside secure facilities in which no network devices are ever allowed. So, it needs to work in either condition. And really, it's absurd that the method cannot work in an unnetworked environment - a major slip-up by MS that is about to force us to embedded linux.</p> <p> </p> <p>So, two questions:</p> <p>1. Is there a way to fix this such that it will fallback correctly and look for local instances when no network is available?</p> <p>2. If not, is there a known way to &quot;trick&quot; a machine into thinking a network is there so this will run correctly, such as some sort of network driver that emulates a connected network?</p>Thu, 29 Mar 2007 18:40:21 Z2009-10-24T21:06:10Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/79766b37-602e-4447-9196-9587bbaf2e9bhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/79766b37-602e-4447-9196-9587bbaf2e9bJohn Tianhttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20Tianfaile copy database on same sql server (2005) error out on user or role even with CopyAllRoles set false<span style="font-size: xx-small;"> <p>I am running this code as script task in ssis 2005<br />here is my code:<br />-----------<br /><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Public</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Sub</span></span><span style="font-size: x-small;"> Main()&nbsp;<font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> dbSourceName </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">String</span></span><span style="font-size: x-small;"> = </span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"YSCMII"</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> dbDestName </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">String</span></span><span style="font-size: x-small;"> = </span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"YSCMII_COPY"&nbsp;</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> conn </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">New</span></span><span style="font-size: x-small;"> ServerConnection<font size="2"> <p>conn.LoginSecure =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">False</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>conn.Login =</p> </font></span> <p><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"user"</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>conn.Password =</p> </font></span> <p><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"password"</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>conn.ServerInstance =</p> </font></span> <p><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"slcsqltest1"</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Connect to the remote SQL Server.</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> srv </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> Server<font size="2"> <p>srv =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">New</span></span><span style="font-size: x-small;"> Server(conn)<font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'The actual connection is made when a property is retrieved.</span></span></p> <span style="font-size: x-small;"> <p>Console.WriteLine(srv.Information.Version)&nbsp;</p> <font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Reference the source database</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> db </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> Database <p>db = srv.Databases(dbSourceName)</p> <font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Create a new database that is to be destination database.</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> dbCopy </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> Database<font size="2"> <p>dbCopy =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">New</span></span><span style="font-size: x-small;"> Database(srv, dbDestName) <p>&nbsp;dbCopy.Create()&nbsp;</p> <font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Define a Transfer object and set the required options.</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Dim</span></span><span style="font-size: x-small;"> xfr </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">As</span></span><span style="font-size: x-small;"> Transfer<font size="2"> <p>xfr =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">New</span></span><span style="font-size: x-small;"> Transfer(db)<font size="2"> <p>xfr.CopyAllTables =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">True</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>xfr.Options.WithDependencies =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">True</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>xfr.Options.ContinueScriptingOnError =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">True</span></span></p> <span style="font-size: x-small;"> <p>xfr.DestinationDatabase = dbCopy.Name</p> <p>xfr.DestinationServer = srv.Name</p> <font size="2"> <p>xfr.DestinationLoginSecure =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">True</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>xfr.CopyAllRoles =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">False</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>xfr.CopySchema =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">True</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Include data</span></span></p> <span style="font-size: x-small;"><font size="2"> <p>xfr.CopyData =</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">False<font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;">'Execute the transfer</span></span></p> <span style="font-size: x-small;"> <p>xfr.TransferData()&nbsp;</p> <p>Dts.TaskResult = Dts.Results.Success</p> <font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">End</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">Sub<font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span></p> <p>------------<br /><br />Error I got as following:<br /><br />script Task: The script threw an exception: ERROR : errorCode=-1073548784 description=Executing the query "EXEC dbo.sp_grantdbaccess @loginame = N'ppenzance', @name_in_db = N'ppenzance'</p> <p>" failed with the following error: "User or role 'ppenzance' already exists in the current database.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.</p> <p>helpFile= helpContext=0 idofInterfaceWithError={8BDFE893-E9D8-4D23-9739-DA807BCDC2AC}</p> <p>Task failed: Script Task<br /><br />Thanks!</p> </span>Fri, 16 Oct 2009 18:23:08 Z2009-10-23T17:48:35Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/b71430cb-05f4-4aaf-a4eb-b150b7cd3146http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/b71430cb-05f4-4aaf-a4eb-b150b7cd3146John Tianhttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20TianSMO could not be used copy sql 2000 database?<p>Does any one know if SMO api&nbsp;should or should not be used to copy sql 2000 database?<br /><br />If not what I should use.</p> <p>I tried use it in ssis script task, and it did not work for me. I don't know if it is supported or just something I am doing wrong.</p> <p>&nbsp;</p> <p>Thanks!</p>Fri, 16 Oct 2009 21:59:51 Z2009-10-22T17:56:35Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fe8bc643-48d1-49d2-8e90-178f10abdf0chttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/fe8bc643-48d1-49d2-8e90-178f10abdf0cDBAJDShttp://social.msdn.microsoft.com/Profile/en-US/?user=DBAJDSNumberOfReplayThreads<p align=left><font face=Arial size=2></font> </p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">I’m attempting to replace a trace that ran for 50 minutes I want the replay to simulate as close as possible to the original.<span style="">  </span>I’m not sure but I was thinking that increasing the number of threads would facilitate this.<span style="">  </span>During the original collection there were anywhere between 700-1000 users.<span style="">  </span>Is there a best way to simulate this?<span style="">  </span>I’m also not sure if I’m using the best replay mode considering what I’m attempting to do.</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> System;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> System.Data;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> System.Collections;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> Microsoft.SqlServer.Management.Common;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> Microsoft.SqlServer.Management.Smo;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">using</span><span style="font-size:10pt;font-family:'Courier New'"> Microsoft.SqlServer.Management.Trace;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">class</span><span style="font-size:10pt;font-family:'Courier New'"> <span style="color:#2b91af">Program</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span><span style="color:blue">static</span> <span style="color:blue">void</span> Main(<span style="color:blue">string</span>[] args)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">TraceReplayOptions</span> to = <span style="color:blue">new</span> <span style="color:#2b91af">TraceReplayOptions</span>();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">TraceReplay</span> tr = <span style="color:blue">new</span> <span style="color:#2b91af">TraceReplay</span>();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">TraceFile</span> tf = <span style="color:blue">new</span> <span style="color:#2b91af">TraceFile</span>();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>to.NumberOfReplayThreads = 1000;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>to.Mode = <span style="color:#2b91af">ReplayMode</span>.ConnectionLevelSync;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>tf.InitializeAsReader(<span style="color:#a31515">@&quot;C: \tracesTest\tracesTest\bin\Debug\test.trc&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>tr.Source = tf;</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>tr.Connection = <span style="color:blue">new</span> <span style="color:#2b91af">SqlConnectionInfo</span>(<span style="color:#a31515">&quot;test&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>tr.ReplayEvent += <span style="color:blue">new</span> <span style="color:#2b91af">ReplayEventHandler</span>(tr_ReplayEvent);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span>tr.Start();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#2b91af">Environment</span>.NewLine + <span style="color:#a31515">&quot;Press any key to continue.&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">Console</span>.ReadKey();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span><span style="color:blue">static</span> <span style="color:blue">void</span> tr_ReplayEvent(<span style="color:blue">object</span> sender, <span style="color:#2b91af">ReplayEventArgs</span> args)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span>{</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;--- Record number: &quot;</span> + args.RecordNumber + <span style="color:#a31515">&quot; ---&quot;</span>);</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:blue">for</span> (<span style="color:blue">int</span> i = 0; i &lt; args.CurrentRecord.FieldCount; i++)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="color:#2b91af">Console</span>.WriteLine(args.CurrentRecord<img height=19 alt=Idea src="http://forums.microsoft.com/MSDN/emoticons/emotion-55.gif" width=19>.ToString());</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">        </span><span style="color:#2b91af">Console</span>.WriteLine();</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">    </span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">}</span><span style="font-size:10pt;font-family:Arial"></span></p>Mon, 08 Sep 2008 15:22:00 Z2009-10-22T13:17:40Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/546b5ce7-3787-4bc8-990f-529c3b6184c2http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/546b5ce7-3787-4bc8-990f-529c3b6184c2Anand.Ranganathanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Anand.Ranganathan.NET 3.0 WF : Accessing "WorkFlowInstance" from an Activity within the workflow<p align=left><font face=Arial size=2></font> </p> <p>The problem is , Im creating a Windows Sequential Workflow which just has only two &quot;Code Activities&quot; . Now Im trying to persist the workflow at the end of the first Activity . So that before commencing the second activity I have the WorkFlow persisted . But  workflow will get persisted only on calling the 'WorkflowInstance.Unload()' method , but im not able to call this method at the activity level. </p> <p align=left> </p> <p align=left>Im using &quot;SqlServerPersistenceStorage&quot; for persistence. </p> <p align=left> </p> <p align=left>Please help me with this. !!!!!!</p>Mon, 08 Oct 2007 09:30:17 Z2009-10-20T08:03:26Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/11df304e-be53-49b5-9aec-2c7b991e4083http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/11df304e-be53-49b5-9aec-2c7b991e4083Gurmit Teotiahttp://social.msdn.microsoft.com/Profile/en-US/?user=Gurmit%20TeotiaFailed to transfer primary key and foreign keyHello,<br /><br />We're trying to transfer database from SQL 2005 to SQL 2008 with following code-<br /><br /><br /><span style="font-size: x-small; color: #2b91af;"><font size="2" color="#2b91af"> <p>Server</p> </font></span> <p><span style="font-size: x-small;"> srcSrv = </span><span style="font-size: x-small; color: #0000ff;">new</span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #2b91af;">Server</span><span style="font-size: x-small;">(</span><span style="font-size: x-small; color: #a31515;">"einpxp102\\sqlexpress"</span><span style="font-size: x-small;">);<font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #2b91af;">Server</span><span style="font-size: x-small;"> dstSrv = </span><span style="font-size: x-small; color: #0000ff;">new</span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #2b91af;">Server</span><span style="font-size: x-small;">(</span><span style="font-size: x-small; color: #a31515;">"einpxp102"</span><span style="font-size: x-small;">); <p>&nbsp;</p> <font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;">// Get source database from the source server.</span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #2b91af;">Database</span><span style="font-size: x-small;"> srcDb = srcSrv.Databases[srcDatabaseName];<font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;">// Create backup databse on the destination server</span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #2b91af;">Database</span><span style="font-size: x-small;"> dstDb = </span><span style="font-size: x-small; color: #0000ff;">new</span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #2b91af;">Database</span><span style="font-size: x-small;">(dstSrv, dstDatabaseName);<font size="2"> <p>dstDb.Create();</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;">// This creates the new database on the 'destination' server.</span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;">// Create transfer.</span></p> <span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #2b91af;">Transfer</span><span style="font-size: x-small;"> t = </span><span style="font-size: x-small; color: #0000ff;">new</span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #2b91af;">Transfer</span><span style="font-size: x-small;">(srcDb);<font size="2"> <p>t.CopyAllObjects =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;">true</span><span style="font-size: x-small;">;<font size="2"> <p>t.CopySchema =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;">true</span><span style="font-size: x-small;">;<font size="2"> <p>t.CopyData =</p> </font></span></p> <p><span style="font-size: x-small; color: #0000ff;">true</span><span style="font-size: x-small;">;<font size="2"> <p>&nbsp;</p> </font></span></p> <p><span style="font-size: x-small; color: #008000;">// Set the destination table for the transfer object.</span></p> <span style="font-size: x-small;"> <p>t.DestinationDatabase = dstDb.Name;</p> <font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;">// Now set up the destination server</span></p> <span style="font-size: x-small;"> <p>t.DestinationServer = dstSrv.Name;</p> <font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #008000;">// and to the transfer, this should do it.</span></p> <span style="font-size: x-small;"> <p>t.TransferData();<br /><br />But it is not transferring primary keys and foreign keys. I'd appreciate any help on it. <br /><br />Regards,<br />Gurmit</p> </span>Thu, 15 Oct 2009 05:30:43 Z2009-10-18T07:00:13Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/bc11ee17-c636-4b7b-91f4-198e61dfa7e4http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/bc11ee17-c636-4b7b-91f4-198e61dfa7e4AlexBB - Vista Ult64 SqlSer64 WinSer64http://social.msdn.microsoft.com/Profile/en-US/?user=AlexBB%20-%20Vista%20Ult64%20SqlSer64%20WinSer64Server backup<p>I've got this code to backup my dbs that seemed to work but the last time I did the backup was a few months ago and now I cannot seem to run it. I get an exception at the statemetn I marked:  NullReferenceException, although I am suer that the data and log folders are put in correctly.<br/><br/>Much of this code was given to me by Alok and I again entreat to him to make sense out of it. I am too preoccupied with other problems to do it myself.<br/><br/>        public void backupDBFull ( string dbName )<br/>        {<br/>            ProvideSecurity.provideSecurity ( );<br/>            DoServer.getSrvAndConnOrSwitch ( dbName );<br/>            BackupDevice dev = new BackupDevice ( );<br/>            dev.Name = &quot;SqlBackUp Default Instance_&quot; + dbName;<br/>            UserDefinedSqlServerData data = ( UserDefinedSqlServerData )Globals.srv.UserData;<br/>            if ( Globals.srv.BackupDevices[ &quot;SqlBackUp Default Instance_&quot; + dbName ] == null )  // &lt;== Exception here.<br/>            {<br/>                Globals.srv.BackupDirectory = ( ( UserDefinedSqlServerData )Globals.srv.UserData ).dataFolder;<br/>                dev = new BackupDevice ( Globals.srv, &quot;SqlBackUp Default Instance_&quot; + dbName );<br/>                dev.BackupDeviceType = BackupDeviceType.Disk;<br/>                dev.PhysicalLocation = Globals.srv.BackupDirectory + dbName + &quot;.bak&quot;;<br/>                dev.Initialize ( );<br/>                dev.Create ( );<br/>            }<br/>            foreach ( Database db in Globals.srv.Databases )<br/>            {<br/>                if ( db.Name.ToUpper ( ).IndexOf ( dbName.ToUpper ( ) ) != -1 )<br/>                {<br/>                    int recoverymod = 0;<br/>                    recoverymod = ( int )db.DatabaseOptions.RecoveryModel;<br/>                    Microsoft.SqlServer.Management.Smo.Backup bk = new Backup ( );<br/>                    bk.Action = BackupActionType.Database;<br/>                    bk.BackupSetDescription = &quot;Full backup of &quot; + dbName;<br/>                    bk.BackupSetName = &quot;sqlBack for &quot; + dbName + &quot;_1&quot;;<br/>                    bk.Database = dbName;<br/>                    BackupDeviceItem bdi = default ( BackupDeviceItem );<br/>                    bdi = new BackupDeviceItem ( dev.Name, DeviceType.LogicalDevice );<br/>                    //Add the device to the Backup object. <br/>                    bk.Devices.Add ( bdi );<br/>                    bk.Checksum = true;<br/>                    string dated = DateTime.Now.Date.ToString ( &quot;MM-dd-yy&quot; );<br/>                    bk.BackupSetName = &quot;BackupFull_&quot; + dated;<br/>                    bk.ContinueAfterError = false;<br/>                    bk.ExpirationDate = DateTime.Now.AddDays ( 1825 );<br/>                    bk.MediaDescription = &quot;Disk&quot;;<br/>                    bk.FormatMedia = false;<br/>                    bk.Incremental = false;<br/>                    bk.LogTruncation = BackupTruncateLogType.Truncate;<br/>                    bk.Information += new ServerMessageEventHandler ( back_Information );<br/>                    bk.Complete += new ServerMessageEventHandler ( back_Complete );<br/>                    bk.Initialize = true;<br/>                    bk.PercentCompleteNotification = 10;<br/>                    bk.PercentComplete += new PercentCompleteEventHandler ( bk_PercentComplete );</p> <p>                    bk.SqlBackupAsync ( Globals.srv );<br/>                    // Inform the user that the backup has been completed. <br/>                    Console.WriteLine ( &quot;Full Backup complete.&quot; );<br/>                    // Remove the backup device from the Backup object. <br/>                    bk.Devices.Remove ( bdi );<br/>                }<br/>            }<br/>        }                                               // backupDBFull<br/><br/>        public class UserDefinedSqlServerData<br/>        {<br/>            public string dataFolder = @&quot;G:\DataBackup\&quot;;<br/>            public string logFolder = @&quot;G:\LogBackup\&quot;;<br/>        } </p> <br/>Thanks.<br/><br/> <hr class=sig> AlexBSun, 27 Sep 2009 22:53:03 Z2009-10-15T23:31:35Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/cc930c8f-3ce6-44b7-b84e-a32c98a20014http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/cc930c8f-3ce6-44b7-b84e-a32c98a20014Manoj Pasumarthihttp://social.msdn.microsoft.com/Profile/en-US/?user=Manoj%20PasumarthiHow to Generate Script for a table using SMO when the table is in Transaction<p><font face="Courier New, Courier, Monospace">Hi,</font></p> <p><font face="Courier New"></font> </p> <p><font face="Courier New">I am facing a problem when i am trying to generate script for a table.The altered table is in transaction.</font></p> <p><font face="Courier New"></font> </p> <p><font face="Courier New">The following is the sample code:</font></p> <p><font face="Courier New"></font> </p><font face="Courier New"><font color="#2b91af" size=2> <p> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div><font face="Courier New"><font color="#2b91af" size=2> <p>SqlConnection</font><font size=2> con = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SqlConnection</font><font size=2>(</font><font color="#a31515" size=2>&quot;server=.;database=TestSMO;uid=sa;pwd=sa2005&quot;</font><font size=2>);</p> <p>con.Open();</p> <p></font><font color="#2b91af" size=2>SqlTransaction</font><font size=2> trans = con.BeginTransaction(</font><font color="#2b91af" size=2>IsolationLevel</font><font size=2>.ReadUncommitted);</p> <p></font><font color="#2b91af" size=2>SqlCommand</font><font size=2> cmd = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SqlCommand</font><font size=2>(</font><font color="#a31515" size=2>&quot;ALTER TABLE Table1 ADD Col3 varchar(20) null&quot;</font><font size=2>, con);</p> <p>cmd.Transaction = trans;</p> <p>cmd.ExecuteNonQuery();</p> <p> </p> <p></p> <p></font><font color="#008000" size=2>// Get the bind token of the transaction related to the supplied connection</p></font><font size=2> <p></font><font color="#008000" size=2>//</p></font><font size=2> <p></font><font color="#2b91af" size=2>SqlCommand</font><font size=2> cmd1 = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SqlCommand</font><font size=2>(</font><font color="#a31515" size=2>&quot;sp_getbindtoken&quot;</font><font size=2>, con, trans);</p> <p>cmd1.CommandType = </font><font color="#2b91af" size=2>CommandType</font><font size=2>.StoredProcedure;</p> <p></font><font color="#2b91af" size=2>SqlParameter</font><font size=2> paramBindToken = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>SqlParameter</font><font size=2>(</font><font color="#a31515" size=2>&quot;@token&quot;</font><font size=2>, </font><font color="#2b91af" size=2>SqlDbType</font><font size=2>.VarChar, 255);</p> <p>paramBindToken.Direction = </font><font color="#2b91af" size=2>ParameterDirection</font><font size=2>.Output;</p> <p>cmd1.Parameters.Add(paramBindToken);</p> <p>cmd1.ExecuteNonQuery();</p> <p></font><font color="#0000ff" size=2>string</font><font size=2> sBindtoken = paramBindToken.Value.ToString();</p> <p></font><font color="#0000ff" size=2>string</font><font size=2> sCmd = </font><font color="#a31515" size=2>&quot;&quot;</font><font size=2>;</p> <p></font><font color="#0000ff" size=2>if</font><font size=2> ((sBindtoken != </font><font color="#0000ff" size=2>null</font><font size=2>) &amp;&amp; (sBindtoken.Length &gt; 0))</p> <p>{</p> <p></font><font color="#008000" size=2>// Bind to the transaction of the connection to the current </p></font><font size=2> <p></font><font color="#008000" size=2>// connection</p></font><font size=2> <p></font><font color="#008000" size=2>//</p></font><font size=2> <p>sCmd = </font><font color="#a31515" size=2>&quot;EXEC sp_bindsession '&quot;</font><font size=2> + sBindtoken + </font><font color="#a31515" size=2>&quot;'&quot;</font><font size=2>;</p> <p></font><font color="#008000" size=2>//svr.ExecuteImmediate(sCmd, SQLDMO.SQLDMO_EXEC_TYPE.SQLDMOExec_Default, sCmd.Length);</p></font><font size=2> <p></p> <p>}</p> <p> </p> <p></font><font color="#2b91af" size=2>ServerConnection</font><font size=2> svrCon = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>ServerConnection</font><font size=2>(</font><font color="#a31515" size=2>&quot;.&quot;</font><font size=2>,</font><font color="#a31515" size=2>&quot;sa&quot;</font><font size=2>,</font><font color="#a31515" size=2>&quot;sa2005&quot;</font><font size=2>);</p> <p></p> <p></font><font color="#2b91af" size=2>Server</font><font size=2> server = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>Server</font><font size=2>(svrCon);</p> <p></font><font color="#2b91af" size=2>Database</font><font size=2> db = server.Databases[</font><font color="#a31515" size=2>&quot;TestSMO&quot;</font><font size=2>];</p> <p></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(db.GetTransactionCount(</font><font color="#2b91af" size=2>TransactionTypes</font><font size=2>.Both));</p> <p>db.ExecuteNonQuery(sCmd,</font><font color="#2b91af" size=2>ExecutionTypes</font><font size=2>.Default);</p> <p></font><font color="#2b91af" size=2>Table</font><font size=2> table = db.Tables[</font><font color="#a31515" size=2>&quot;Table1&quot;</font><font size=2>];</p> <p></font><font color="#2b91af" size=2>ScriptingOptions</font><font size=2> options = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>ScriptingOptions</font><font size=2>();</p> <p>options.Default = </font><font color="#0000ff" size=2>true</font><font size=2>;</p> <p></font><font color="#2b91af" size=2>StringCollection</font><font size=2> sc = table.Script(options);</p> <p></font><font color="#0000ff" size=2>foreach</font><font size=2> (</font><font color="#0000ff" size=2>string</font><font size=2> str </font><font color="#0000ff" size=2>in</font><font size=2> sc)</p> <p>{</p> <p></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(str);</p> <p>}</p> <p>trans.Commit();</p> <p>con.Close();</p></font></font></div></div> <p> </p></font> <p><font face="Courier New, Courier, Monospace">I am able to achive the above using SQL-DMO but with the new SMO i am not able to.</font><font face="Courier New, Courier, Monospace">Can any please help me how do i generate script for a table which is altered in a transaction.</font></p><font size=2> <p></p></font></font>Sat, 30 Jun 2007 09:11:52 Z2009-10-17T08:27:40Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/ab268525-5919-4176-9a4d-085f4f833defhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/ab268525-5919-4176-9a4d-085f4f833defAlexBB - Vista Ult64 SqlSer64 WinSer64http://social.msdn.microsoft.com/Profile/en-US/?user=AlexBB%20-%20Vista%20Ult64%20SqlSer64%20WinSer64Changing field typeI have a db with numerous tables each having "volume" field. Historically long ago I started assigning some of the tables Int value for the field but then changed my mind and continued adding tables&nbsp;with BigInt. Now when I iterate thru the tables with my C# code I get in trouble. Of course I can check for the type and take action but I want to resolve the issue once and for all. How can I do it programmatically with Smo? <br /><br />Thanks.<hr class="sig">AlexBTue, 13 Oct 2009 23:57:35 Z2009-10-14T18:20:47Zhttp://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/165524ad-90e5-4894-bcc0-3d7b2eb0aa15http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/165524ad-90e5-4894-bcc0-3d7b2eb0aa15Slodejhttp://social.msdn.microsoft.com/Profile/en-US/?user=Slodejuserdefined Function wit SMO get Error Msg 6522Hello I am trying to create a userdefined function that works with SMO. I wrote this class and function in C #. This will generate the Create Script for an available table, then drop the table and execute the generate script. I managed to create DLL to generate a assembly and a userdefined function of it in SQl Server. When i try to use this function i get an error.<br /> I work with SqlServer 2008 and Visual C# 2005 Express.<br /> <br /> <em>Message 6522, Level 16, State 1, Line 3</em> <br /> <em>A .NET Framework error occurred during execution of user-defined routine or aggregate &quot;genScript&quot;: <br /> System.Security.SecurityException:The assembly does not allow callers who are not fully trusted.</em> <br /> <em>System.Security.SecurityException: <br /> &nbsp;&nbsp; bei ScriptGen.Program.genScript()</em> <br /> <br /> I Create the Assembly with persmissions_set = safe, unsafe and external acces, but nothing work.<br /> <br /> I were very grateful if someone could help me further<br /> <br /> Here is my C# Class with Function<br /> <pre lang="x-c#">using System; using System.Collections.Generic; using System.Text; using Microsoft.SqlServer.Management.Smo; using System.Data; using System.Data.Sql; using System.Data.SqlTypes; using System.Data.SqlClient; using System.Collections.Specialized; using System.Collections; namespace ScriptGen { public class Program { public static SqlString genScript() { Server srv = new Server(&quot;Test_server&quot;); //SQL Server connection string Source database Database sourceDB = srv.Databases[&quot;Test_DB&quot;]; Table t = sourceDB.Tables[&quot;test_table&quot;]; StringCollection k = t.Script(); t.Drop(); foreach (String s in k) { sourceDB.ExecuteNonQuery(s); //Console.WriteLine(s); } return &quot;test&quot;; } } } </pre> <br /> <br /> <br /> <br />Wed, 07 Oct 2009 12:58:14 Z2009-10-19T06:35:29Z