Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / May 2008

Tip: Looking for answers? Try searching our database.

SQL Mobile 2005 Script

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matheu - 26 May 2008 13:13 GMT
Hi, my question is if possible to create a script of a database, and run it
when i do the deploy of the application on the device ?....

Any link or sample to do it ?

Thanks a lot!
Simon Hart [MVP] - 26 May 2008 14:37 GMT
Usally the best place to put his code is in the BLL of the app. This allows a
nicer migration when your schema changes. Steve Lasker has some really cool
demos and ideas regarding this, check out his blog.

Also note, if you are using any version of Microsoft SQL Server Compact (not
sure about other products) you can't simply run a script which contains
multiple queries as the engine can only process one query at a time.

I've just copied this code from an app I'm working on a present. It simply
loads a script Tables_Create.sql stored in a resx file, if a database doesn't
not already exist it goes off any creates it from scratch:

               //Now for the SQL.
               StringBuilder sql = new StringBuilder();
               List<string> commands = new List<string>();
               System.IO.StringReader sr = new
System.IO.StringReader(Properties.StoredProcs.Tables_Create);
               while (true)
               {
                   string s = sr.ReadLine();
                   if (s == null)
                       break;

                   if (s.Length >= 2)
                   {
                       if (s.ToUpper().Substring(0, 2) != "GO")
                           sql.Append(s);
                       else
                       {
                           commands.Add(sql.ToString());
                           sql = new StringBuilder();
                       }
                   }
               }

               //Execute each statement in turn.
               foreach (string s in commands)
               {
                   cmd.CommandText = s;
                   connection.ExecuteNonQuery(cmd, null);
               }          

The query might look like:

CREATE TABLE Person (
            PersonId int IDENTITY(1,1)
                              CONSTRAINT PersonId_PK PRIMARY KEY,
           AddressId int NULL,
                      Name nvarchar(50),
          ChangeStamp rowversion NOT NULL)
GO

CREATE INDEX Name_IX ON Person(Name)
GO

ALTER TABLE Person
           ADD CONSTRAINT Name_FK
           FOREIGN KEY(Name)
           REFERENCES Address(AddressId)
           ON DELETE CASCADE
GO

The code divides the query up, you get the idea. Of course the above is
wrapped in a transaction.
Signature

Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com

> Hi, my question is if possible to create a script of a database, and run it
> when i do the deploy of the application on the device ?....
>
> Any link or sample to do it ?
>
> Thanks a lot!
Hosmerica - 26 May 2008 17:10 GMT
> Usally the best place to put his code is in the BLL of the app. This
> allows a
[quoted text clipped - 72 lines]
>>
>> Thanks a lot!

This is good solid advice, Simon.  I've run scripts on a database the same
way shown here with only slight variations.
Simon Hart [MVP] - 26 May 2008 18:40 GMT
Sure, the cool thing about this is you can take that script and actually run
it through the management studio without change against a compact database or
mdf desktop database if you wanted. This also allows a nice abstraction of
SQL from your C# code. Although there is an exception to running the script
via management studio in that of course SQL compact 3.5 is not supported on
the management studio 2005, we will have to wait for server 2008 for that
which is currently in beta.
Signature

Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com

> > Usally the best place to put his code is in the BLL of the app. This
> > allows a
[quoted text clipped - 75 lines]
> This is good solid advice, Simon.  I've run scripts on a database the same
> way shown here with only slight variations.
Hosmerica - 26 May 2008 19:00 GMT
> Sure, the cool thing about this is you can take that script and actually
> run
[quoted text clipped - 7 lines]
> the management studio 2005, we will have to wait for server 2008 for that
> which is currently in beta.

I maintain an application in VS2003 (not the 1.1 compact framework) which
applies scripts for DB updates periodically in very much the same way as
you've shown.  The script(s) is 100% runnable against SQL Server (2000 and
2005).  I haven't worked with SQL compact at all, so I'm unaware of any
differences with that and SQL Server (2000 or 2005).  It is wickedly cool
stuff.
Simon Hart [MVP] - 26 May 2008 18:31 GMT
Sorry I meant data access layer not business layer.
Signature

Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com

> Usally the best place to put his code is in the BLL of the app. This allows a
> nicer migration when your schema changes. Steve Lasker has some really cool
[quoted text clipped - 67 lines]
> >
> > Thanks a lot!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.