Sample Ado SQL
Sample Ado SQL
aspx
In this walkthrough, you use Visual Studio to create a small database that contains the sample code for Walkthrough:
Creating a Simple Data Application by Using ADO.NET.
In this topic
Prerequisites
To complete this walkthrough, you must have Visual Studio 2012 installed. You must also be able to connect to a database
server or a LocalDB database on which you have permissions to create and deploy a database.
3. In the Templates list, choose Sql File, and then choose the Open button.
4. Copy the following Transact-SQL code, and then paste it into the Transact-SQL Editor.
1 of 6 20/10/2017, 2:58 PM
Walkthrough: Creating a Small Sample Database https://msdn.microsoft.com/en-us/library/jj943771(v=vs.110).aspx
2 of 6 20/10/2017, 2:58 PM
Walkthrough: Creating a Small Sample Database https://msdn.microsoft.com/en-us/library/jj943771(v=vs.110).aspx
UPDATE [Sales].[Orders]
SET [Status] = 'X'
WHERE [OrderID] = @OrderID;
UPDATE [Sales].[Customer]
SET
YTDOrders = YTDOrders - @Delta
WHERE [CustomerID] = @CustomerID
COMMIT TRANSACTION
END
GO
PRINT N'Creating Sales.uspFillOrder...';
GO
CREATE PROCEDURE [Sales].[uspFillOrder]
@OrderID INT, @FilledDate DATETIME
AS
BEGIN
DECLARE @Delta INT, @CustomerID INT
BEGIN TRANSACTION
SELECT @Delta = [Amount], @CustomerID = [CustomerID]
FROM [Sales].[Orders] WHERE [OrderID] = @OrderID;
3 of 6 20/10/2017, 2:58 PM
Walkthrough: Creating a Small Sample Database https://msdn.microsoft.com/en-us/library/jj943771(v=vs.110).aspx
UPDATE [Sales].[Customer]
SET
YTDSales = YTDSales + @Delta
WHERE [CustomerID] = @CustomerID
COMMIT TRANSACTION
END
GO
PRINT N'Creating Sales.uspNewCustomer...';
GO
CREATE PROCEDURE [Sales].[uspNewCustomer]
@CustomerName NVARCHAR (40),
@CustomerID INT OUTPUT
AS
BEGIN
INSERT INTO [Sales].[Customer] (CustomerName) VALUES (@CustomerName);
SET @CustomerID = SCOPE_IDENTITY();
RETURN @@ERROR
END
GO
PRINT N'Creating Sales.uspPlaceNewOrder...';
GO
CREATE PROCEDURE [Sales].[uspPlaceNewOrder]
@CustomerID INT, @Amount INT, @OrderDate DATETIME, @Status CHAR (1)='O'
AS
BEGIN
DECLARE @RC INT
BEGIN TRANSACTION
INSERT INTO [Sales].[Orders] (CustomerID, OrderDate, FilledDate, Status, Amount)
VALUES (@CustomerID, @OrderDate, NULL, @Status, @Amount)
SELECT @RC = SCOPE_IDENTITY();
UPDATE [Sales].[Customer]
SET
YTDOrders = YTDOrders + @Amount
WHERE [CustomerID] = @CustomerID
COMMIT TRANSACTION
RETURN @RC
END
GO
CREATE PROCEDURE [Sales].[uspShowOrderDetails]
@CustomerID INT=0
AS
BEGIN
SELECT [C].[CustomerName], CONVERT(date, [O].[OrderDate]), CONVERT(date,
[O].[FilledDate]), [O].[Status], [O].[Amount]
FROM [Sales].[Customer] AS C
INNER JOIN [Sales].[Orders] AS O
ON [O].[CustomerID] = [C].[CustomerID]
WHERE [C].[CustomerID] = @CustomerID
END
4 of 6 20/10/2017, 2:58 PM
Walkthrough: Creating a Small Sample Database https://msdn.microsoft.com/en-us/library/jj943771(v=vs.110).aspx
6. In the File Name box, enter SampleImportScript.sql, and then choose the Save button.
Save the file to any location on your computer, but make note of the location because you must use it in the script
in the next procedure.
Next, create a database project, and then import the schema from the script that you've created.
2. Under Installed, expand the Templates node, expand the Other Languages node, choose the SQL Server
category, and then choose the SQL Server Database Project template.
Note
The Other Languages node doesn’t appear in all installations of Visual Studio.
4. Select the Create directory for solution check box if it isn't already selected.
5. Clear the Add to source control check box if it isn't already cleared, and then choose the OK button.
2. On the Welcome page, review the text, and then choose the Next button.
3. Choose the Single File option button, and then choose the Browse button.
5 of 6 20/10/2017, 2:58 PM
Walkthrough: Creating a Small Sample Database https://msdn.microsoft.com/en-us/library/jj943771(v=vs.110).aspx
The script is imported, and the objects that the script defines are added to your database project.
6. Review the summary, and then choose the Finish button to close the Import SQL Script File dialog box.
7. In Solution Explorer, verify that the script files (.sql) were imported into the Sales, Scripts, and Security folders of
your project.
8. In SQL Server Object Explorer, verify that the database appears under the Projects node.
At this point, the database contains only system objects, such as tables and stored procedures. After you deploy the
database, it will contain the user tables and stored procedures that the scripts define.
© 2017 Microsoft
6 of 6 20/10/2017, 2:58 PM