62,051
社区成员
发帖
与我相关
我的任务
分享
namespace Models
{
[Serializable]
[Entity("Models.Users", "Users")]
[EntityID(EntityIDType.Identity, "Id")]
public class Users
{
private string userCode = string.Empty;
[Field("UserNo",typeof(string),30,1)]
public string UserCode
{
get { return userCode; }
set { userCode = value; }
}
private string userName = string.Empty;
[Field("UserName",typeof(string),30,2)]
public string UserName
{
get { return userName; }
set { userName = value; }
}
private int id;
[Field("ID", typeof(int), 30,true,true)]
public int Id
{
get { return id; }
set { id = value; }
}
private string departCode;
[Field("DepartNo", typeof(string))]
public string DepartCode
{
get { return departCode; }
set { departCode = value; }
}
private string departName = string.Empty;
[Field("departName", typeof(string),false,false,true)]
[Relation(typeof(Departments), "DepartCode", RelationType.OneToMany)]
public string DepartName
{
get { return departName; }
set { departName = value; }
}
private string address;
[Field("address", typeof(string), false, false, true)]
[Relation(typeof(Departments), "DepartCode", RelationType.OneToMany)]
public string Address
{
get { return address; }
set { address = value; }
}
}
private void btnInsert_Click(object sender, EventArgs e)
{
Users user=new Users();
user.UserCode = "200001";
user.UserName = "TestModelORM";
user.DepartCode = "000002";
EntityFactory factory = new EntityFactory();
if (factory.InsertObject<Users>(user))
{
MessageBox.Show(user.Id.ToString());
}
//info.RemoteService<bool>("BusinessProcess","InsertEntity",new object[]{user});
}
string sqlInsert="insert into table(.....)values (......);select SCOPE_IDENTITY() "
SqlCommand command=new SqlCoomand(你的连接,sqlInsert);
command.CommandType = CommandType.Text;
int primaryKeyID=command.ExecuteScalar();
.....
if exists (select name from sysobjects
where name = 'pr存储过程名称' and type = 'p')
drop procedure pr存储过程名称
go
CREATE PROCEDURE pr存储过程名称
@Cname nvarchar(50), --产品类名称
@Cx1 nvarchar(50), --产品类说明
@Pname nvarchar(50), --产品名称
@Px1 nvarchar(50), --产品名称
@intPID int output, --返回产品表自动编号
@intCID int output --返回产品类表自动编号
AS
SET NOCOUNT ON
Declare @intErrorCode int
Select @intErrorCode = @@Error
If @intErrorCode = 0
Begin Transaction
If @intErrorCode = 0
Begin
insert into 表B
(Cname,Cx1)
VALUES
(@Cname,@Cx1)
Select @intCID = @@identity
insert into 表A
(Pname,PclassID,Px1)
VALUES
(@Pname,@intCID,@Px1)
Select @intPID = @@identity
Select @intErrorCode = @@Error
End
If @intErrorCode = 0 and @@trancount > 0
Commit Transaction
Else
Rollback Transaction
Return @intErrorCode
Go