Skip to main content

Posts

Showing posts with the label SQL 2008

Run a SQL Script at runtime of a C# application

In my previous post I explained how to run a SQL Script using Command prompt. It is bit difficult. So I found a way to run the SQL script using the application. I used it on button click. Here is how I did it. 1.   Create your SQL Script. 2. Add the references below to your C# application using Microsoft.SqlServer.Server; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc; 3. Add the path to your SQL Script string Filename = Path .Combine( Environment .GetFolderPath( Environment . SpecialFolder .MyDocuments) + "\\Configuration\\" , "Create_Database.sql" ); 4. Add the code below to read your SQL Script StreamReader StreamReader;           StreamReader = new StreamReader (Filename); string CreateDBQuery = StreamReader.ReadToEnd(); 5. Create a connection with server & Execute Query string tempConnString= "Data Source=(local);Inte...

SQL Script to Create Tables

Continued from previous post “ SQL Script to Create Database ” IF NOT EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID (N ' [dbo].[Product] ' ) AND type in (N 'U' )) BEGIN CREATE TABLE [dbo].[Product](     [ProductID] [int] PRIMARY KEY IDENTITY (1,1) NOT NULL,     [Name] [varchar](50) NULL,     [Description] [varchar](100) NULL,     [Modified] [timestamp] NOT NULL ) END GO SET IDENTITY_INSERT [dbo].[Product] ON INSERT [dbo].[Product] ([ProductID], [Name], [Description]) VALUES (1, N 'Red Bull' , N 'This drink gives you wings.' ) INSERT [dbo].[Product] ([ProductID], [Name], [Description]) VALUES (2, N 'Orange Juice' , N 'This drink goes better with Red Bull.' ) INSERT [dbo].[Product] ([ProductID], [Name], [Description]) VALUES (3, N 'BBQ Chicken' , N 'Yummy chicken for your tummy.' ) INSERT [dbo].[Product] ([ProductI...

SQL Script to Create Database

USE master GO if exists ( select * from sysdatabases where name =' MyDatabase ')         drop database Chinthaka go DECLARE @device_directory NVARCHAR (520) SELECT @device_directory = SUBSTRING ( filename , 1, CHARINDEX ( N 'master.mdf' , LOWER ( filename )) - 1) FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1 EXECUTE (N' CREATE DATABASE MyDatabase   ON PRIMARY (NAME = N' ' MyDatabase ' ', FILENAME = N ' ' ' + @device_directory + N' MyDatabase .mdf' ')   LOG ON (NAME = N' ' MyDatabase _log' ',  FILENAME = N ' ' ' + @device_directory + N' MyDatabase .ldf ' ' ) ' ) go exec sp_dboption ' MyDatabase ','trunc. log on chkpt.','true' exec sp_dboption ' MyDatabase ','select into/bulkcopy','true' GO set quoted_identifier on GO USE [...

Microsoft SQL Server 2008 Management Studio Express

Microsoft offers a free tool, the Microsoft SQL Server 2008 Management Studio Express, as a convenient means of managing and editing entries in SQL databases through a GUI. The SQL Server 2008 license comes with the full version of the Management Studio, but the Express version is still very functional for the day-to-day operations. However, finding the stand-alone version of the software is quite a pain. Most people don't need to install a SQL server on their machine, but that is the most promoted means of getting the Management Studio Express. You can download it from here https://www.microsoft.com/downloads/details.aspx?FamilyID=08e52ac2-1d62-45f6-9a4a-4b76a8564a2b&displayLang=en ( 32-bit (168.3 MB) or 64-bit (176.5 MB)) Before you Install: Microsoft .Net Framework 3.5 SP1 Windows Installer 4.5 Windows PowerShell 1.0 How to Install: Once the installer starts, select "Installation" from the left column. The options on the right will change to mat...