如何以编程方式或手动方式生成包含insert语句和数据的sql文件?

时间:2010-01-04 23:42:59

标签: c# sql-server

我在c#中处理项目,我想从我的数据库生成数据并在需要时将其传递给服务器,因此我可以在服务器上运行此文件,我可以生成此文件来自c#? 以及如何从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

任何建议,提前谢谢:) 达尼

1 个答案:

答案 0 :(得分:0)

SSMS Tools Pack是SQL Server Management Studio的一个插件,它具有许多有用的功能,包括从表数据生成插入脚本。