一旦我将其写入数据库,如何更新类? [英] How Do I Update A Class Once I've Written It To A Database?
问题描述
大家好
我正在写一个遥测程序来跟踪比赛的所有信息。
每场比赛是一个在Session表中记录它自己。
数据库表包含以下字段:
[ SessionID](主键)
[日期]
[位置](外键)
[驱动程序](外键)
[评论]
[EngineSetup]
[GearingSetup]
[TyreSetup]
[ BrakeSetup]
[SkyConditions]
[温度]
[WindSpeed]
[下雨]
我的C#应用程序中的Session类具有相同的字段,减去数据库生成的SessionID。
在我的Session类中有一个Create()方法,它调用
DATA ACESS LAYER中的方法,该方法执行存储过程以在会话表中创建记录。
我的问题是这个:
一旦我解雇了Session.Create()方法,无论如何都要检索数据库生成的sessionID并将其分配给同一个会话宾语?
我的Session类中有一个构造函数,可以使用SessionID作为Session对象进行编辑。
Hi All
I'm writing a telemetry program which keeps track of all information from a race.
each race is a record on it's own within the Session table.
the database table contains the following fields:
[SessionID](Primary Key)
[Date]
[Location](Foreign Key)
[Driver](Foreign Key)
[Comments]
[EngineSetup]
[GearingSetup]
[TyreSetup]
[BrakeSetup]
[SkyConditions]
[Temperature]
[WindSpeed]
[Rain]
The Session class in my C# application has the same fields, minus the SessionID which is generated by the Database.
Within my Session class there is a Create()method, which calls a method in the
DATA ACESS LAYER, which in executes the stored procedure to create the record in the Session Table.
My question is this:
once I've fired off the Session.Create() method, is there anyway to retrieve the sessionID generated by the database and assign it to the same session object?
I have a constructor in my Session class that can take a Session object with a SessionID for editing purposes.
推荐答案
亲爱的,
Dear,
/* 1. Add below into your parameter list in store procedure */
@SessionID <datatype> OUTPUT
/* 2. Write down your insert statement */
/* 3. Set last inserted identity to declared variable */
SET @SessionID = @@IDENTITY
</datatype>
您的C#代码如下:
Your C# code as below :
string returnVal;
SqlParameter myValue = new SqlParameter();
myValue.ParameterName = "@SessionID";
myValue.Direction = ParameterDirection.Output;
myValue.DbType = <your dbtype as per table structure>;
myCmd.Parameters.Add(myValue);
myCmd.ExecuteNonQuery();
returnVal = Convert.ToString(myValue.Value);
这篇关于一旦我将其写入数据库,如何更新类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!