|
Hi, With a stored proc in SQL Server 2008 Express, I am using a Merge which may perform any or all of Update, Insert, and Delete statements on a table. How can I display the output of all actions in a DataGridView in a .Net Windows Form Application? More to the point, how do I direct the data from SQL to my app? I'm not sure what, if any, code you may need to see, please ask if it is necessary. Thank you, Mike | | mwalsh62 Tuesday, September 08, 2009 11:20 AM | Hi,
If you want to run storedProcedure,you need to setthe commandType to StoredProcedure and set the command text to the stored Procedure name.
CommandType Enumeration
http://msdn.microsoft.com/en-us/library/system.data.commandtype(VS.80).aspx
Please refer to the following KB. If anything unclear, please feel free to tell me.
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET
http://support.microsoft.com/kb/310070/en-us
How To Call a Parameterized Stored Procedure by Using ADO.NET and Visual Basic .NET
http://support.microsoft.com/kb/308049/en-us
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorWednesday, September 16, 2009 11:43 AM
- Marked As Answer bymwalsh62 Wednesday, September 16, 2009 8:07 PM
- Unmarked As Answer bymwalsh62 Wednesday, September 16, 2009 8:07 PM
- Unmarked As Answer bymwalsh62 Wednesday, September 16, 2009 8:07 PM
- Unmarked As Answer bymwalsh62 Wednesday, September 16, 2009 8:07 PM
- Unmarked As Answer bymwalsh62 Wednesday, September 16, 2009 8:06 PM
-
| | Ling Wang Tuesday, September 15, 2009 11:13 AM | Hi, Thanks for your response. I am having a hard time wrapping my brain around this. Here is vb calling Stored Procedure:
Public Sub UploadGuides()
Dim LCPdb As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=LC_Pricing;" & _
"Integrated Security=true;")
Using LCPdb
Try
LCPdb.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand()
cmd.Connection = LCPdb
cmd.CommandType = Data.CommandType.StoredProcedure
If frmMain.chkUpUS.Checked = True Then
cmd.CommandText = "Upload_US_LC"
End If
If frmMain.chkUpSysco.Checked = True Then
cmd.CommandText = "Upload_Sysco_LC"
End If
If frmMain.chkUpPFG.Checked = True Then
cmd.CommandText = "Upload_PFG_LC"
End If
Using reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
End Using
Catch ex As Exception
GeneralError("UploadGuides", ex)
Finally
LCPdb.Close()
LCPdb = Nothing
End Try
End Using
End Sub
And here is Stored Procedure:
USE [LC_Pricing]
GO
/****** Object: StoredProcedure [dbo].[Upload_US_LC] Script Date: 09/16/2009 09:58:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Upload_US_LC]
AS
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USFoodsTemp]')
AND type in (N'U'))
DROP TABLE [dbo].[USFoodsTemp]
CREATE TABLE [dbo].[USFoodsTemp] (
[Product #] Int,
Description VARCHAR(255),
Brand VARCHAR(255),
Price Money,
Store TinyInt,
Selected Bit
)
INSERT INTO [dbo].[USFoodsTemp] ( [Product #], [Description], [Brand], [Price], Store, Selected)
SELECT [Product Number], [Product Desc], [Product Brand], [Product Price], 1, 0
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Shopping List 2009.xls;HDR=YES',
'SELECT * FROM [Shopping List 2009$]')
MERGE INTO USFoods AS Target
USING USFoodsTemp AS Source
ON Target.[Product #]=Source.[Product #]
WHEN MATCHED AND Target.Description <> Source.Description OR Target.Price <> Source.Price THEN
UPDATE SET Target.Description = Source.Description, Target.Last_Price = Target.Price,
Target.Price=Source.Price, Target.Updated = GetDate()
WHEN NOT MATCHED BY Target THEN
INSERT ([Product #], [Description], [Brand], [Price], Store, Selected, Updated)
VALUES (Source.[Product #],Source.[Description],Source.[Brand], Source.[Price], 1, 0, GETDATE())
WHEN NOT MATCHED BY Source AND Target.[Product #] <> 0 THEN
DELETE
OUTPUT $action,
DELETED.[Product #] AS Product#,
DELETED.[Description] AS ProductName,
DELETED.[Price] AS Price,
INSERTED.[Product #] AS Product#,
INSERTED.[Description] AS ProductName,
INSERTED.[Price] AS Price;
SELECT @@ROWCOUNT;
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[USFoodsTemp]')
AND type in (N'U'))
DROP TABLE [dbo].[USFoodsTemp]
END
GO<br/><br/>
I wish to return results something like those from the OUTPUT above. I just can't get the gist of what parameters need be where. If anyone one could hold my hand for just a moment, and give me an example of what both the vb and sql parameters would be for one of the above output results, I will be forever in your debt, at least spiritually. Thank you, Mike | | mwalsh62 Wednesday, September 16, 2009 3:09 PM |
|