我做了這樣的函數如何在方法中返回字符串值?
public int InsertData(CategoryPhoto catphoto)
{
string ans = null;
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "prcCategoryPhoto";
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.Add(new SqlParameter("@PhotoID", prdctphoto.PhotoID));
cmd.Parameters.Add(new SqlParameter("@PhotoName", catphoto.PhotoName));
//cmd.Parameters.Add(new SqlParameter("@LeftPhoto", prdctphoto.LeftPhoto));
//cmd.Parameters.Add(new SqlParameter("@RightPhoto", prdctphoto.RightPhoto));
//cmd.Parameters.Add(new SqlParameter("@BackPhoto", prdctphoto.BackPhoto));
//cmd.Parameters.Add(new SqlParameter("@MaterialPhoto", prdctphoto.MaterialPhoto));
cmd.Parameters.Add(new SqlParameter("@ExtName", catphoto.ExtName));
cmd.Parameters.Add(new SqlParameter("@PhotoType", catphoto.PhotoType));
cmd.Parameters.Add(new SqlParameter("@PhotoSize", catphoto.PhotoSize));
cmd.Parameters.Add(new SqlParameter("@CategoryID", catphoto.CategoryID));
ans = cmd.ExecuteScalar().ToString();
//var result = cmd.ExecuteScalar();
//ans = int.Parse(result.ToString());
cmd.Dispose();
DataConnection.CloseConnection();
return ans;
}
在我的存儲過程是
create proc [dbo].[prcCategoryPhoto]
(
@PhotoName Varchar(100),
@ExtName Varchar(100),
@PhotoType Varchar(100),
@PhotoSize int,
@CategoryID varchar(20)
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID)
values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@CategoryID)
select @@IDENTITY
上寫return ans
它給一個錯誤 不能隱式轉換字符串爲int
和書寫
return int.Parse(ans);
它給出的例外是nvarchar不能轉換爲int
可以讓我們知道你在'ans'得到什麼。? –
你桌子上的你的身份專欄是什麼?什麼數據類型? – Szymon