我寫了一個存儲過程返回一個字符串:從存儲過程C#
USE [database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [database table]
@StoreNum INT,
@CustomerID INT,
@r VARCHAR(100) OUTPUT
AS
BEGIN
Select @r = Notes FROM [Database table]
WHERE CustomerID = @CustomerID
AND StoreNumber = CAST(@StoreNum AS NVARCHAR(10))
END
當我運行在SQL Server Management Studio中,@r返回我期待字符串此存儲過程。
但是,下面的C#代碼中出現錯誤:「無法從int轉換爲字符串」。
SqlParameter param4 = new SqlParameter("@StoreNum", storeaudit.StoreNumber);
SqlParameter param5 = new SqlParameter("@CustomerID", "9");
SqlParameter param7 = new SqlParameter("@r", "");
param5.SqlDbType = System.Data.SqlDbType.Int;
param7.Direction = System.Data.ParameterDirection.Output;
var NoteMan = db.Database.ExecuteSqlCommand("sp_GetNotesData @StoreNum, @CustomerID, @r", param4, param5, param7);
String managers = param7.Value.ToString();
System.Console.WriteLine(param7.Value);
任何幫助將不勝感激,謝謝
更新 - 增加了輸出和SQLTYPE的參數,現在字符串經理返回「」。
杜佩? http://stackoverflow.com/questions/290652/get-output-parameter-value-in-ado-net –
當你使用字符串類型的輸出參數時,有必要設置'size'和direction。你確實設定了方向但不是大小。如果你沒有設置大小,你可能只會得到單個字符 - 我看到更新 - 你什麼都沒有。繼續並設置大小 –