如何获得最后输入的ID? [英] How to get last entered id?
本文介绍了如何获得最后输入的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经制作了一个商店程序来获取最新输入的ID。有用。但在c#编码中,它显示零值。我想最后输入id。请帮忙。
我尝试过:
I have made a store procedure to get latest entered id. It works. But in c# coding, it displays zero value. I want to get last entered id. Please help.
What I have tried:
CREATE PROCEDURE spDonor (
@flag char(1), @donorcode varchar(2), @donor varchar(25), @lastid int out
) WITH ENCRYPTION AS
SET NOCOUNT OFF
BEGIN
IF @flag = 'I' BEGIN
INSERT INTO tblDonor (DonorCode, Donor) VALUES (@donorcode, UPPER(@donor))
SELECT @lastid = SCOPE_IDENTITY()
END
IF @flag = 'U' BEGIN
UPDATE tblDonor
SET DonorCode = @donorcode, Donor = UPPER(@donor) WHERE SeqNo = @lastid END
IF @flag = 'D' BEGIN
DELETE tblDonor WHERE SeqNo = @lastid END
IF @flag = 'A' BEGIN
SELECT SeqNo, DonorCode, Donor FROM tblDonor END
IF @flag = 'S' BEGIN
SELECT SeqNo, DonorCode, Donor FROM tblDonor WHERE SeqNo = @lastid END
IF @flag = 'N' BEGIN
SELECT SeqNo, DonorCode, Donor FROM tblDonor WHERE (UPPER(Donor) Like '%'+UPPER(@donor)+'%') END
END
SET NOCOUNT ON
RETURN
GO
///
private string _flag = string.Empty;
public string Flag
{
get { return _flag; }
set { _flag = value; }
}
private string _dnrCode = string.Empty;
public string DnrCode
{
get { return _dnrCode; }
set { _dnrCode = value; }
}
private string _donor = string.Empty;
public string Donor
{
get { return _donor; }
set { _donor = value; }
}
private int _lastid = 0;
public int Lastid
{
get { return _lastid; }
set { _lastid = value; }
}
public int executeDonor()
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@flag", _flag)
, new SqlParameter("@donorcode", _dnrCode)
, new SqlParameter("@donor", _donor)
, new SqlParameter("@lastid", _lastid)
};
return DAO.ExecuteProc("spDonor", param);
}
private void btnSave_Click(object sender, EventArgs e)
{
mtacsys.Flag = "N";
mtacsys.Donor = txtDonor.Text.Trim();
DataTable dtchk = mtacsys.GetDonorByCodeName();
if (dtchk.Rows.Count > 0)
{
toolStripMessage.Text = mtacsys.Donor + " : This is already entered.";
}
else
{
mtacsys.Flag = "I";
mtacsys.DnrCode = txtDonorId.Text.Trim();
mtacsys.Donor = txtDonor.Text.Trim();
int i = mtacsys.executeDonor();
if (i > 0)
{
toolStripMessage.Text = "Record Saved Successfully";
ResetTextBox();
MessageBox.Show(mtacsys.Lastid + " : is entered");
}
}
推荐答案
检查这个 sql server - 在C#中使用存储过程输出参数 - Stack Overflow [ ^ ]
读取c#中out参数的值,你必须更改ExecuteProc
方法。
参考此示例代码:
check this sql server - Using stored procedure output parameters in C# - Stack Overflow[^]
to read the value from out parameter in c#, you will have to change theExecuteProc
method.
refer this sample code:
public int executeDonor()
{
int lastID = 0;
SqlConnection con = new SqlConnection("Your Conn string");
SqlCommand cmd = new SqlCommand("spDonor ", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@flag", _flag));
cmd.Parameters.Add(new SqlParameter("@donorcode", _dnrCode));
cmd.Parameters.Add(new SqlParameter("@donor", _donor));
SqlParameter paramOutId = new SqlParameter("@lastid", System.Data.SqlDbType.Int);
cmd.Parameters.Add(paramOutId);
paramOutId.Direction = System.Data.ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
lastID = (int)paramOutId.Value;
con.Close();
}
您已经知道最后一个ID - 您保存在其中@LASTID
当您执行插入时:
You already know the last ID - you saved in it@LASTID
when you did the insert:
INSERT INTO tblDonor (DonorCode, Donor) VALUES (@donorcode, UPPER(@donor))
SELECT @lastid = SCOPE_IDENTITY()
只需返回。
Just return that.
这篇关于如何获得最后输入的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文