我有一个数据实体对象驱动一个将数据插入本地SQL表的存储过程。麻烦的是我最终在我的表中使用了零而不是我正在推动的整数值。将创建行,并正确填充字符串列。这是一个双层项目,具有Web客户端前端和IIS托管的wcf服务,负责管理数据层。如果我使用WCF测试客户端,通过Visual Studio调试,调用插入,数据将完美地推进。如果我单步执行Web客户端,我可以在类对象中看到传递给WCF服务调用的正确值赋值。两者之间似乎出现了一些问题,我显然没有看到这棵树的树木。
这是数据类:
[DataContract]
public class CategoryClass
{
[DataMember]
public Int32 ID { get; set; }
[DataMember]
public string Category { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public Int32 Position { get; set; }
}
这是服务:
public Int32 InsertCategory(CategoryClass CategoryItem)
{
try
{
using (var context = new SQLRepositoryEntities())
{
return context.Database.SqlQuery<Int32>("exec InsertCategory @Category, @Description, @Position",
new SqlParameter { ParameterName = "@Category", SqlDbType = System.Data.SqlDbType.NVarChar, Value = CategoryItem.Category },
new SqlParameter { ParameterName = "@Description", SqlDbType = System.Data.SqlDbType.NVarChar, Value = CategoryItem.Description },
new SqlParameter { ParameterName = "@Position", SqlDbType = System.Data.SqlDbType.Int, Value = CategoryItem.Position }).Single();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
以下是该服务的网络电话:
SQLRepositoryItems.RepositoryItems repo = new SQLRepositoryItems.RepositoryItems();
foreach (DataRow row in table.Rows)
{
CategoryClass category = new CategoryClass();
category.Category = row["Name"].ToString();
category.Position = Int32.Parse(row["Category_Id"].ToString());
category.Description = "";
int iOut;
bool bOut;
try
{
repo.InsertCategory(category, out iOut, out bOut);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
...这里是存储过程:
ALTER PROCEDURE [dbo].[InsertCategory]
@CATEGORY nvarchar(50),
@DESCRIPTION nvarchar(500),
@POSITION int
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [SQLRepository].[dbo].[RepositoryCategories]
([CATEGORY]
,[DESCRIPTION]
,[POSITION])
VALUES
(@CATEGORY,
@DESCRIPTION,
@POSITION)
DECLARE @ID int
SET @ID = SCOPE_IDENTITY()
SELECT @ID
END
GO
答案 0 :(得分:0)
问题是我在界面中声明方法的方式。我原来的声明是:
[OperationContract]
Int32 InsertCategory(CategoryClass Category);
将声明更改为以下更正了问题:
[OperationContract, XmlSerializerFormat(Style=OperationFormatStyle.Rpc)]
Int32 InsertCategory(CategoryClass Category);