我做了这样的功能
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 的异常