使用Web服务方法将数据插入数据库,主键自动递增

时间:2012-04-20 16:16:15

标签: c# asp.net database web-services web

我遇到有关自动增量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。

那么,我该怎么做才能将这个WS方法与自动增量PK ID一起使用?

2 个答案:

答案 0 :(得分:0)

你能给出你正在使用的dal代码吗? 如果我看着你的异常然后它告诉id有身份,你给id一个值是错误的id将被自动添加。你没有必要设置考试 错误发生在Query不在webmethod

答案 1 :(得分:0)

更改为

sampleWS s = new sampleWS();
s.addExam(100000000, bla, bla);

它有效..

感谢您的参与...