【问题标题】:inserting Via Output Parameter C#通过输出参数插入 C#
【发布时间】:2013-02-12 17:10:41
【问题描述】:

我有一个表单,可以通过单击将数据发送到多个表, 人、教育、经验、参考。

如何将数据添加到多个表中,以便所有表中都有相同的人员 ID,

我是新手,使用分层架构,所有数据库代码都在 DAL 中,) 使用 Asp 2.0

我正在考虑使用插入 Person 的存储过程并获取 PersonID 作为输出参数,但我不知道如何使用该输出参数插入到其他表中。

我的示例代码是这样的

public virtual bool AddJobApplication(int JobID, string Name, string FatherName, string Phone, string Email, string Address, int AppID) {

            obj_db2.objCmd.CommandText = "AddApplication";
            obj_db2.objCmd.CommandType = CommandType.StoredProcedure;
            obj_db2.objCmd.Parameters.AddWithValue("JjobID", JobID);
            obj_db2.objCmd.Parameters.AddWithValue("@Name", Name);
            obj_db2.objCmd.Parameters.AddWithValue("@FName", FatherName);
            obj_db2.objCmd.Parameters.AddWithValue("@Email", Email );
            obj_db2.objCmd.Parameters.AddWithValue("@Address", Address);
            obj_db2.objCmd.Parameters.AddWithValue("@Phone", Phone);
            SqlParameter param = new SqlParameter("@AppID", AppID);
            param.Direction = ParameterDirection.Output;
            param.ParameterName = "@AppID";
            param.DbType = DbType.Int32;
            obj_db2.objCmd.Parameters.Add(param);
            obj_db2.objCmd.ExecuteNonQuery();

}

我在 DAL 中有这个函数,我将参数作为输出参数,我知道我可以从后面的代码访问这个 AddJobApplication 以输入表单信息,但没有得到如何使用这个输出插入的要点,对不起,如果我的水平很低,但我需要帮助。

【问题讨论】:

  • 你能澄清一下吗?您是在问如何从 C# 中的存储过程中获取输出参数?
  • 不,我在问如何使用输出参数插入到其他表中。
  • 请发布您尝试过的相关代码,并尝试澄清您的问题。即,预期行为与实际行为等。
  • 我有请检查我在数据访问层中调用参数,但问题是我的输出参数在 DAL 中,我通过后面的表单代码提交表单数据,所以我如何访问前面的输出参数层,

标签: c# asp.net sql stored-procedures


【解决方案1】:

这实际上取决于已建立的架构。

如果更简单,您可能可以只返回新记录的标识,而不是使用输出参数:

INSERT INTO Person (
    FirstName,
    LastName
)
VALUES (
    'James',
    'Johnson'
)

SELECT @@IDENTITY --return the identifier of the new record

如果您要更新多个表,请确保所有这些都在单个事务的范围内完成。

【讨论】:

  • 我为此使用了几个存储过程。一个存储过程将 ID 作为输出参数返回给我,然后我如何使用此 ID 插入不同的表?我想在文件后面的代码中访问这个输出参数,,
【解决方案2】:

存储过程是解决此问题的正确方法。存储过程到位后,您将在插入方法中定义输出参数。

下面是一个示例,说明如何插入博客文章然后取回 SQL 创建的 ID。

示例(插入博客文章):

 public int InsertArticle(ArticleItem articleitem)
    {
        SqlParameter articleid = new SqlParameter(Parameters.ArticleID, SqlDbType.Int);
        articleid.Direction = ParameterDirection.Output;

        SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, INSERT_ARTICLE,
                                  new SqlParameter(Parameters.Body, articleitem.Body),
                                  new SqlParameter(Parameters.Category, articleitem.Category),
                                  new SqlParameter(Parameters.ExpireDate, articleitem.ExpireDate),
                                  new SqlParameter(Parameters.PublishDate, articleitem.PublishDate),
                                  new SqlParameter(Parameters.Published, articleitem.Published),
                                  new SqlParameter(Parameters.Section, articleitem.Section),
                                  new SqlParameter(Parameters.Title, articleitem.Title),
                                  articleid);
        return (int)articleid.Value;
    }

调用代码

public void InsertAllTheThings()
{
    var articleId = InsertArticle(articleItem); //Insert an article, return the ID
    //Once you have the id, use it as needed.
    var data = new SomeAwesomeData();
    data.ArticleID = articleId;
    data.IsAwesome = true;
    InsertSomeRelatedData(data);
}   

【讨论】:

  • 谢谢你,但是所有这些输出参数都在 DataAccessLayer 中,但我想在表单的代码隐藏文件中访问这个输出参数,所以我可以使用输出 ID 添加到多个表中,任何想法??
  • 正如您在代码示例中看到的,它返回 ArticleID。然后,您可以在任何需要的地方使用该 ID。如果您在多层应用程序中工作,则不会从表单调用其他插入或更新,而是从 DAL 执行此操作。
  • 谢谢,我确实知道使用此输出参数通过 DAL 插入的想法,但是我正在获取要添加到 Databse 中的表单值,就像 UI->BLL->DAL,我是困惑和新手,但我需要你的专家帮助。
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2012-01-25
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
相关资源
最近更新 更多