C# 如何转换此方法以捕获插入的id?

C# 如何转换此方法以捕获插入的id?,c#,.net,winforms,C#,.net,Winforms,我有一个windows窗体,我在按钮单击事件中插入值,如下所示 Candidate CanObj = new Candidate(txtName.Text); if (new CandidateOP().saveCandidate(CanObj)) { MessageBox.Show("NEW candidate details added"); } 这是我的业务层方法 public Boolean saveCandidate(Candidate CanObj) { strin

我有一个windows窗体,我在按钮单击事件中插入值,如下所示

Candidate CanObj = new Candidate(txtName.Text);
if (new CandidateOP().saveCandidate(CanObj))
{
    MessageBox.Show("NEW candidate details added");
}
这是我的业务层方法

public Boolean saveCandidate(Candidate CanObj)
{
    string query6 = "EXEC insertToCand01'" + CanObj.NIC + "'";
    return (new DataAccessLayer().executeNonQueries(query6));
}
这是我的数据访问层方法

public Boolean executeNonQueries(string query02)
{
    Boolean flag = false;
    SqlConnection con = null;
    SqlCommand com = null;
    try
    {
        con = new SqlConnection(DBConnect.makeConnection());
        con.Open();
        com = new SqlCommand(query02, con);
        com.ExecuteNonQuery();
        flag = true;
    }
    catch (Exception ex)
    {
        flag = false;
        throw ex;
    }
    finally
    {
        com.Dispose();
        con.Close();
    }
    return flag;
}
这是要插入的存储过程中的查询。 在我的表格中,ID设置为自动递增

INSERT INTO Candidate (User_Name) VALUES (@Uname);
现在我想显示插入时要显示的插入ID。 所以我像这样修改了查询

INSERT INTO Candidate (User_Name) OUTPUT INSERTED.User_ID VALUES (@Uname);  
我想更改我的数据访问层和业务层以恢复价值 如何改变我的数据访问层来实现这一点


提前感谢。

简单但重要的一点是:您应该真正使用参数化查询来避免SQL注入问题,并使用适当的ORM系统

关于您的具体问题:使用ExecuteScalar而不是ExecuteOnQuery调用您的过程,并从存储过程返回生成的id

实际上,您不需要SP,例如,您可以执行
select scope\u identity()
。或者,您可以在SP中使用输出参数。但只返回标量是最简单的方法。

类似以下内容:

Candidate CanObj = new Candidate(txtName.Text);
int id = new CandidateOP().saveCandidate(CanObj);
/* You have **id** here, and you can use it. */
if (id >= 0)
{
   MessageBox.Show("NEW candidate details added");
}
业务层:

public Boolean saveCandidate(Candidate CanObj)
{
    string query6 = "EXEC insertToCand01'" + CanObj.NIC + "'";
    return new DataAccessLayer().executeNonQueries(query6);
}
public int executeNonQueries(string query02)
{
    long id = -1;
    SqlConnection con = null;
    SqlCommand com = null;
    try
    {
        con = new SqlConnection(DBConnect.makeConnection());
        con.Open();
        com = new SqlCommand(query02, con);
        SqlParameter returnParameter = com.Parameters.Add("RetVal", SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        com.ExecuteNonQuery();
        id = (int) returnParameter.Value;
    }
    catch (Exception ex)
    {
        id = -1;
        throw ex;
    }
    finally
    {
        com.Dispose();
        con.Close();
    }
    return id;
}
和您的访问层:

public Boolean saveCandidate(Candidate CanObj)
{
    string query6 = "EXEC insertToCand01'" + CanObj.NIC + "'";
    return new DataAccessLayer().executeNonQueries(query6);
}
public int executeNonQueries(string query02)
{
    long id = -1;
    SqlConnection con = null;
    SqlCommand com = null;
    try
    {
        con = new SqlConnection(DBConnect.makeConnection());
        con.Open();
        com = new SqlCommand(query02, con);
        SqlParameter returnParameter = com.Parameters.Add("RetVal", SqlDbType.Int);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        com.ExecuteNonQuery();
        id = (int) returnParameter.Value;
    }
    catch (Exception ex)
    {
        id = -1;
        throw ex;
    }
    finally
    {
        com.Dispose();
        con.Close();
    }
    return id;
}

我不喜欢你浪费新实例的方式:(@fejecjoco如果我错了,请纠正我,因为我读到这是最好的方法,因为如果你使用并行执行计划,这是最好的方法。关于sql注入。是的,我知道。在这方面它是不必要的,因为它是我私人使用的。请不要只关注这一点。请提高你对转换方法的关注度。我不仅专注于此。我写了关于ExecuteScalar的文章,我相信这是最简单的方法。@Ali Sepheri.Kh如何在变量中获取表单级别的id?能否提供一个代码示例,如何将其捕获到表单级别的int类型变量中。@Sahil:对不起,我不可用。我编辑了我的答案。你的意思是这样吗?是这样的。业务layer方法应该是int。我这样做了,也尝试了,但不起作用。将0作为新id。