SQL Server और C# 4.0 पर संग्रहीत प्रक्रिया का उपयोग करते समय, मैं OUTPUT जानकारी पुनर्प्राप्त करने और चयन कथन से जानकारी वापस करने में असमर्थ हूं। मैं "वस्तु संदर्भ किसी ऑब्जेक्ट के उदाहरण पर सेट नहीं हो रहा" प्राप्त करता रहता हूं। जब मैं ExecuteScalar() करता हूं तो मुझे पंक्तियां मिलती हैं, लेकिन डेटा नहीं। वहां कुछ उदाहरण मिले और वे मेरे जैसा काम कर रहे हैं, इसलिए मुझे लगता है कि मुझे मेरे सामने कुछ आसान याद आ रहा है। धन्यवाद।SQL आउटपुट संग्रहीत प्रक्रिया ExecuteReader
संग्रहित प्रक्रिया
USE [PhoneDb]
GO
/****** Object: StoredProcedure [dbo].[TestPagingProcedure] Script Date: 06/16/2011 08:39:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TestPagingProcedure]
-- Add the parameters for the stored procedure here
@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @first_id UNIQUEIDENTIFIER
DECLARE @startRow int
SET @startRowIndex = (@startRowIndex - 1) * @maximumRows
IF @startRowIndex = 0
SET @startRowIndex = 1
SET ROWCOUNT @startRowIndex
SELECT @first_id = ExtensionGUID FROM ExtItem ORDER BY ExtensionGUID
PRINT @first_id
SET ROWCOUNT @maximumRows
SELECT
ExtensionGUID, AesExt, AesHashPassword, ToNumber, AgentExt, Name, JpgImageName, BigImageName, WbmpImageName
FROM ExtItem WHERE
ExtensionGUID >= @first_id
ORDER BY ExtensionGUID
SET ROWCOUNT 0
-- GEt the total rows
SELECT @totalRows = COUNT(ExtensionGUID) FROM ExtItem
END
सी # कोड
public bool GetPagedResults(string startRowIndex, string maxRows, ref double totalRowsReturned)
{
bool IsSuccess = false;
string clearPassword = "";
Log.WriteLine("GetExtList : ENTERED GETEXTITEM: ", Log.DEBUG_LEVEL.VERBOSE);
SqlConnection MyConnection = null;
EnDecrypt hasher = null;
try
{
if (SQLLookup.DatabaseString == "")
{
Log.WriteLine("GetPagedResults : SQLLookup.DatabaseString is empty:", Log.DEBUG_LEVEL.VERBOSE);
SQLLookup.SQLFinder();
Log.WriteLine("GetPagedResults : SQL FINDER RUN: SQLLookup.DatabaseString:'" + SQLLookup.DatabaseString + "'", Log.DEBUG_LEVEL.VERBOSE);
}
Log.WriteLine("GetPagedResults: SQL Server '" + SQLLookup.DatabaseString + "'", Log.DEBUG_LEVEL.VERBOSE);
_extItemList.Clear(); // Keep new records from just being appended to existing list.
hasher = new EnDecrypt("SetMyKey", "SaltGenerator");
// Create a Connection to SQL Server
MyConnection = new SqlConnection(@"Data Source= " + SQLLookup.DatabaseString + @"; Initial Catalog=PhoneDb;Integrated Security=True");
SqlCommand myCommand = new SqlCommand("TestPagingProcedure", MyConnection);
myCommand.CommandType = CommandType.StoredProcedure;
/* ASSIGN PARAMETERS */
myCommand.Parameters.Add(new SqlParameter("@startRowIndex", startRowIndex));
myCommand.Parameters.Add(new SqlParameter("@maximumRows", maxRows));
myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4);
myCommand.Parameters["@totalRows"].Direction = ParameterDirection.Output;
Log.WriteLine("GetPagedResults:3 After try ", Log.DEBUG_LEVEL.VERBOSE);
Log.WriteLine("GetPagedResults:3 startRowIndex = " + startRowIndex + " maxRows = " + maxRows, Log.DEBUG_LEVEL.VERBOSE);
MyConnection.Open();
SqlDataReader Reader = myCommand.ExecuteReader();
Log.WriteLine("GetPagedResults BEFORE WHILE LOOP", Log.DEBUG_LEVEL.VERBOSE);
while (Reader.Read())
{
/* BUILD EXT ITEM*/
ExtItem extItem = new ExtItem();
if (Reader.IsDBNull(0) || Reader.GetGuid(0) == Guid.Empty)
extItem.ExtensionGUID = Guid.Empty;
else
extItem.ExtensionGUID = Reader.GetGuid(0);
if (Reader.IsDBNull(1) || Reader.GetString(1) == "")
extItem.AesExt = "No value";
else
extItem.AesExt = Reader.GetString(1);
/* ADD ITEM TO LIST */
AddItem(extItem);
//Log.WriteLine("GetExtList extItem: " + extItem.ToString(), Log.DEBUG_LEVEL.VERBOSE);
}
// get the total rows
Log.WriteLine("GetPagedResults: New Total number of pages: " + (int)myCommand.Parameters[2].Value, Log.DEBUG_LEVEL.TERSE);
// totalRowsReturned = myCommand.Parameters["@totalRows"];
IsSuccess = true;
MyConnection.Close();
Log.WriteLine("GetPagedResults: RETURNING:", Log.DEBUG_LEVEL.VERBOSE);
}
catch (Exception ex)
{
Log.WriteLine("GetPagedResults: Unable to retrieve Extension list. Caught Exception " + ex.Message,
Log.DEBUG_LEVEL.TERSE);
IsSuccess = false;
}
MyConnection.Close();
return IsSuccess;
}
'SET ROWCOUNT' की बजाय' SELECT TOP (@maximumRows) ... 'का उपयोग करना बेहतर है। इस तरह क्वेरी ऑप्टिमाइज़र * जानता है * कि आप केवल शीर्ष पंक्तियां चाहते हैं और इसके लिए अनुकूलित योजना उत्पन्न कर सकते हैं। –