tags:

views:

68

answers:

1

Hi every one . I'm work on project in c# ,and I want to generate data from my db and pass it to the server just when I need to do that, so I can then run this file on server , Can I generate this file from c#? and how can I generate file like this from sql:

SET NOCOUNT ON
GO

USE master
 GO
IF EXISTS ( SELECT  *
            FROM    sysdatabases
            WHERE   name = 'Northwind' ) 
  DROP DATABASE Northwind
 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 Northwind ON PRIMARY (NAME = N''Northwind'', FILENAME = N''' + @device_directory + N'northwnd.mdf'') LOG ON (NAME = N''Northwind_log'', FILENAME = N''' + @device_directory + N'northwnd.ldf'')')
 go

EXEC sp_dboption 'Northwind', 'trunc. log on chkpt.', 'true'
EXEC sp_dboption 'Northwind', 'select into/bulkcopy', 'true'
 GO

SET quoted_identifier ON
 GO

/* Set DATEFORMAT so that the date strings are interpreted correctly regardless of the default DATEFORMAT on the server. */
SET DATEFORMAT mdy
 GO
USE "Northwind"

GO
IF EXISTS ( SELECT  *
            FROM    sysobjects
            WHERE   id = OBJECT_ID('dbo.Customers')
                    AND sysstat & 0xf = 3 ) 
  DROP TABLE "dbo"."Customers"

CREATE TABLE "Customers"
  ( 
   "CustomerID" nchar(5) NOT NULL
  ,"CompanyName" nvarchar(40) NOT NULL
  ,"ContactName" nvarchar(30) NULL
  ,"ContactTitle" nvarchar(30) NULL
  ,"Address" nvarchar(60) NULL
  ,"City" nvarchar(15) NULL
  ,"Region" nvarchar(15) NULL
  ,"PostalCode" nvarchar(10) NULL
  ,"Country" nvarchar(15) NULL
  ,"Phone" nvarchar(24) NULL
  ,"Fax" nvarchar(24) NULL
  ,CONSTRAINT "PK_Customers" PRIMARY KEY CLUSTERED ( "CustomerID" )
  )
 GO
CREATE INDEX "City" ON "dbo"."Customers"("City")
 GO
CREATE INDEX "CompanyName" ON "dbo"."Customers"("CompanyName")
 GO
CREATE INDEX "PostalCode" ON "dbo"."Customers"("PostalCode")
 GO
CREATE INDEX "Region" ON "dbo"."Customers"("Region")
 GO

ALTER TABLE "Customers" NOCHECK CONSTRAINT ALL
 go
INSERT  "Customers"
VALUES  ( 'ALFKI', 'Alfreds Futterkiste', 'Maria Anders',
          'Sales Representative', 'Obere Str. 57', 'Berlin', NULL, '12209',
          'Germany', '030-0074321', '030-0076545' )
INSERT  "Customers"
VALUES  ( 'ANATR', 'Ana Trujillo Emparedados y helados', 'Ana Trujillo',
          'Owner', 'Avda. de la Constitución 2222', 'México D.F.', NULL,
          '05021', 'Mexico', '(5) 555-4729', '(5) 555-3745' )
INSERT  "Customers"
VALUES  ( 'ANTON', 'Antonio Moreno Taquería', 'Antonio Moreno', 'Owner',
          'Mataderos 2312', 'México D.F.', NULL, '05023', 'Mexico',
          '(5) 555-3932', NULL )
INSERT  "Customers"
VALUES  ( 'AROUT', 'Around the Horn', 'Thomas Hardy', 'Sales Representative',
          '120 Hanover Sq.', 'London', NULL, 'WA1 1DP', 'UK', '(171) 555-7788',
          '(171) 555-6750' )
INSERT  "Customers"
VALUES  ( 'BERGS', 'Berglunds snabbköp', 'Christina Berglund',
          'Order Administrator', 'Berguvsvägen 8', 'Luleå', NULL, 'S-958 22',
          'Sweden', '0921-12 34 65', '0921-12 34 67' )
INSERT  "Customers"
VALUES  ( 'BLAUS', 'Blauer See Delikatessen', 'Hanna Moos',
          'Sales Representative', 'Forsterstr. 57', 'Mannheim', NULL, '68306',
          'Germany', '0621-08460', '0621-08924' )
INSERT  "Customers"
VALUES  ( 'BLONP', 'Blondesddsl père et fils', 'Frédérique Citeaux',
          'Marketing Manager', '24, place Kléber', 'Strasbourg', NULL, '67000',
          'France', '88.60.15.31', '88.60.15.32' )
INSERT  "Customers"
VALUES  ( 'BOLID', 'Bólido Comidas preparadas', 'Martín Sommer', 'Owner',
          'C/ Araquil, 67', 'Madrid', NULL, '28023', 'Spain', '(91) 555 22 82',
          '(91) 555 91 99' )
INSERT  "Customers"
VALUES  ( 'BONAP', 'Bon app''', 'Laurence Lebihan', 'Owner',
          '12, rue des Bouchers', 'Marseille', NULL, '13008', 'France',
          '91.24.45.40', '91.24.45.41' )
INSERT  "Customers"
VALUES  ( 'BOTTM', 'Bottom-Dollar Markets', 'Elizabeth Lincoln',
          'Accounting Manager', '23 Tsawassen Blvd.', 'Tsawassen', 'BC',
          'T2F 8M4', 'Canada', '(604) 555-4729', '(604) 555-3745' )
 go
ALTER TABLE "Customers" CHECK CONSTRAINT ALL

for any suggest ,thanks in advance :) Dani

A: 

The SSMS Tools Pack is a plug-in for SQL Server Management Studio that has a lot of useful functionality, including generation of insert scripts from table data.

Rory
Really I appreciate your quick replay Rory ,thank you for this Add-In ; OK know i wanna ask if i can do that programatically , i mean i want to but button in the main form and when press it well get the data and put them in datatbale or arry then I can pass these data to the server without using sql? is this the best way agian thanks alot and Seasons greetings:)
Dani AM
Season's greetings to you too, Dani. Let me just clarify your question quickly, do you want the button in your program to generate the script file and write it to the user's drive (or maybe just display it to them) or are you looking to have the button push the script you generated manually across to the server and have it execute to populate the database?
Rory
sorry for delay Rory and thanks alot for your replay, in fact I want the first one; the button in my program to generate the script (in .net c# or vb.net), after that I can upload the script to the server.
Dani AM
Rory
That's what I'm looking for ;)Thank you Rory.
Dani AM