我在使用自动增量 ID 的 Web 服务方法方面遇到问题。我的数据库中有一张表:tableExam
PK - IDExam - int - auto increment
---- ExamName - string
---- ExamGrade - int
然后我创建一个 Web 服务方法:
[WebMethod]
public void addExam(int IDExam, string ExamName, int ExamGrade)
{
Exam e = new Exam{
ExamName = ExamName,
ExamGrade = ExamGrade};
}
然后我将它用于我的表单。当我点击提交按钮时,它运行:
sampleWS s = new sampleWS();
s.addExam(bla, bla, bla);
现在有一个错误告诉我无法设置主键。
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'tableExam' when IDENTITY_INSERT is set to OFF.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
我尝试像这样更改我的网络服务:
[WebMethod]
public void addExam(string ExamName, int ExamGrade)
{
Exam e = new Exam{
ExamName = ExamName,
ExamGrade = ExamGrade};
}
现在它仍然有错误:
ObjectDataSource 'ObjectDataSourceP' 找不到具有以下参数的非泛型方法 'addExam':IDExam、ExamName、ExamGrade。
那么,我必须怎么做才能使用这个带有自动递增 PK ID 的 WS 方法呢?