smo

Use smo to rename datafiles

我的梦境 提交于 2019-12-05 03:51:20
问题 How can I use SMO to rename the physical .mdf .ndf .ldf files. This article was helpful but I need to use C# SMO Objects. Using the SMO Server Object I can retrieve the database, then Get Access to the DataFile objects. Per this link. These have a Rename, however after rename, nothing changes. 回答1: Well, I haven't tried this, but as per your first link, you can detach the database, rename the file on disk and then re-attach the database. So, give that a shot. The Server object in SMO has a

Run .sql file from Application in C# using SMO library

我的未来我决定 提交于 2019-12-05 00:32:04
问题 I am building an application in C# visual studio 2012 RC. I have to run the scripts within the application. for this i am using. These two libraries. My application's target .net framework is 4.5. using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; I wrote the code to run the scripts after checking weather database exists or not. When i run the code an unexpected exception pops up. i really know what does it mean. Could you please help me in order to get rid

Cleaning up after calls to SMO Server and Database

廉价感情. 提交于 2019-12-04 08:36:25
How do you make SMO release it's connections? I have this code: public static class SqlServerConnectionFactory { public static Server GetSmoServer() { using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)) { var s = new ServerConnection(c); c.Close(); return new Server(s); } } public static Database GetSmoDatabase(Server server) { var db = server.Databases[ConfigurationManager.AppSettings["Database"]]; db.AutoClose = true; return db; } } Called like this from an ASP.Net MVC app run in IIS...: public ActionResult Index() { server =

Using SMO to copy a database and data

不想你离开。 提交于 2019-12-04 07:50:14
问题 I am trying to make a copy of a database to a new database on the same server. The server is my local computer running SQL 2008 Express under Windows XP. Doing this should be quite easy using the SMO.Transfer class and it almost works! My code is as follows (somewhat simplified): Server server = new Server("server"); Database sourceDatabase = server.Databases["source database"]; Database newDatbase = new Database(server, "new name"); newDatbase.Create(); Transfer transfer = new Transfer

What's the fastest method to check SQL server availability?

一个人想着一个人 提交于 2019-12-04 07:40:24
What's the best method to check if SQL server exists or not? I'm trying Microsoft.SqlServer.Management.Smo.Server.PingSqlServerVersion() and it works fine if server exists and available. But it kinda pretty slow, if there is no such a server. Is there any fast enough method to check without even defining user credentials (only the server name), if a server exists? What do you recommend to use? You could just use TcpClient class to query the server and check if a specific port is open, could be something like this: using System.Net; using System.Net.Sockets; public bool CheckServerAvailablity

What's the best way to detect the presence of SMO?

六月ゝ 毕业季﹏ 提交于 2019-12-04 07:25:48
I have some code that uses SMO to populate a list of available SQL Servers and databases. While we no longer support SQL Server 2000, it's possible that the code could get run on a machine that SQL Server 2000 and not have the SMO library installed. I would perfer to check for SMO first and degrade the functionality gracefully instead of blowing up in the user's face. What is best way to detect whether or not SMO is available on a machine? Every example that I have seen through a quick Google scan was a variation of "look for C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft

SMO and Sql Server 7.0

时光毁灭记忆、已成空白 提交于 2019-12-04 05:49:20
问题 Does anyone have a definitive answer to whether Sql Server Management Objects is compatible with Sql Server 7.0? The docs state: Because SMO is compatible with SQL Server version 7.0, SQL Server 2000, SQL Server 2005, and SQL Server 2008, you easily manage a multi-version environment. But trying to connect to a Sql 7 instance gets me: "This SQL Server version (7.0) is not supported." Has anyone been successful in getting these 2 to play nice? 回答1: you can use SMO to connect to SQL Server

Scripting out individual objects from SQL using SMO

冷暖自知 提交于 2019-12-04 05:25:07
问题 For my job I often have to script out a table with all its keys, constraints and Triggers (basically a full script to recreate the table) from a Microsoft SQL 2008 server.I also have to do this for procedures and triggers. What I do now is open SSMS right click the object and select script to and select to script it to a file. So if I have 3 procedures to do and 10 tables and 1 trigger I end up doing this 14 times . What I would like is a powershell script that I could feed a list of objects

How to generate sql scripts using SMO scripter

青春壹個敷衍的年華 提交于 2019-12-04 03:54:08
My database has tables, views and all. And I need a way to generate SQL script for all the DDL in an automated manner. No need for data. There are FK constraints so table creation scripts should be ordered properly. Some views use another view, so view creation scripts also have to be ordered properly. Starting from a script presented on MSDN Blog , I got the following: function Generate-SqlScript { param( [string]$srvname, [string]$database, [string]$ScriptOutputFileName ) [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null $srv = New-Object ("Microsoft

C# SMO - Scripting Table Data to File. Throwing Error

自闭症网瘾萝莉.ら 提交于 2019-12-04 03:31:43
问题 I'm trying to create a backup of the data contained within my database before I re-create the tables. I've got so far as to connect to the server and create the file to store the SQL. The problem is its throwing an error on the last line. "Object reference not set to an instance of an object" I've been at this project all day so might be missing something that a fresh pair of eyes would help with. Here's the code: public void scriptTables() { string folder = HttpContext.Current.Server.MapPath