Monday, March 12, 2012

Example connecting to SQLExpress file

Has any one got a simple script, or referance to a small example of connecting to a sql express file using smo.Hi - what are you trying to accomplish? Do you mean a database (MDF) file?|||Yes, I want to be able to connect to the SQL File (MDF) and have the ability to script out the Database objects. In doing some research this can not be done with the SQLExpress system and tools that get installed with VS 2005 c# Express or the VS 2005 system. To do it using an IDE or tools system most of the answers I have received have been to install the sql management tools. To me this seems like over kill, and at my place of employment we have 10 developers that will be using the sql express system for several projects and when complete we will add them the mdf files to the main servers. That being the case I do not want to purchase more licences then what I need. With the DMO Objects I used to be able to script out the DDl and create scripts, Also using the VS 2003 system I used to be able to generate scripts, But with the new Product.... I can not find a way.

So this is where the SMO objects come in, I have gone through the Hands on labs for SQL 2005 that I was pointed to at Teched, but these only cover the full version not Express.

All I need is a simple script that will show me how to connect to the Database file. If I could get a simple script that would say return the names of the tables in the file that will get me started.|||Take a look at http://blogs.msdn.com/mwories/articles/smosample_transfer.aspx. This sample explains how to generate a script for a given database with SMO. SMO works with Express, so I do not know what limitation you are running in. Can you elaborate?|||My problem is how to connect to the actual file in the connection objects.

// Setup source connection (in this sample source is .\inst1 and target is '.' (the default instance)
Server svr = new Server(@.".\inst1");
Database db = svr.Databases["testdb"];

With the above code, if I have a Visual Studio project that is located c:\projects\test, and the mdf file is located here c:\projects\test\data\test.mdf.

How do I connect to that file, I want to create an application that will allow me to pass a file location (Like "C:\projects\test\data\test.mdf") and have it script out the Database structure including any Stored procs and such to an SQL file that I can execute against another database at a later stage. But I can not build the connection object to connect to the express file.

I have VS 2005 RC Installed if that helps, but I also plan to use this on the express IDEs (C#, and Web Developer)

|||I have put together this simple script to list the databases on the express server,


Server sqlserver = new Server(".\\SQLExpress");

foreach (Database d in sqlserver.Databases)

{

Console.WriteLine(d.Name);

}


I think that I need to attach the file to the Database engine before the SMO Objects can read it. Could someone please assist in a sample script on how to attach "test.mdf" so that it will appear in the list that the above script returns.

No comments:

Post a Comment