如何获取正在插入的记录的主键?

时间:2016-09-20 16:34:51

标签: c# asp.net

如何获取正在插入的记录的主键,并将其用作另一个Insert Into语句中的外键?我可以使用隐藏字段来存储值吗?如果是这样,我该如何设置隐藏字段的值?

这是一个ASP.NET Web Forms c#应用程序。

我自己测试了文档上传,它的工作方式已经过了。但是现在我需要为docStats表添加第二个Insert Into语句。

以下是现在的代码:

protected void newDocUpldBtn_Click(object sender, EventArgs e)
    {
        //Save the file to the server and set the full path
        int i = 0;
        FileUpload fu = docFU;
        string filename = fu.FileName;
        string fnnnoext = System.IO.Path.GetFileNameWithoutExtension(fu.FileName);
        string fnnextonly = System.IO.Path.GetExtension(fu.FileName);

        if (fu.HasFile)
        {
            while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
            {
                i++;
                filename = (fnnnoext + "(" + i.ToString() + ")" + fnnextonly);
            }
            fu.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);

            hdn_doc_path.Value = (Server.MapPath("~/Data/") + filename);
            hdn_doc_path_no_ext.Value = (fnnnoext + "(" + i.ToString() + ")" + fnnextonly);

            SqlConnection drap_cnxn = new SqlConnection("Data Source=WDBSVCPRD01\\SVCDB;Initial Catalog=drap;Integrated Security=True");
            {
                SqlCommand new_doc_cmd = new SqlCommand("Insert Into docs(docTitle, docType, docContractType, docOrg, docDept, docDesc, PriorContCd, LegCompContId, docUpldDt, docUpldBy, docPath, venIdFk) Values(LTRIM(RTRIM(@docTitle)), LTRIM(RTRIM(@docType)), LTRIM(RTRIM(@docContractType)), LTRIM(RTRIM(@docOrg)), LTRIM(RTRIM(@docDept)), LTRIM(RTRIM(@docDesc)), LTRIM(RTRIM(@PriorContCd)), LTRIM(RTRIM(@LegCompContId)), LTRIM(RTRIM(@docUpldDt)), LTRIM(RTRIM(@docUpldBy)), LTRIM(RTRIM(@docPath)), LTRIM(RTRIM(@venIdFk)))", drap_cnxn);
                new_doc_cmd.Parameters.AddWithValue("@docTitle", docTitleTextBox.Text);
                new_doc_cmd.Parameters.AddWithValue("@docType", docTypeDdl.SelectedValue);
                new_doc_cmd.Parameters.AddWithValue("@docContractType", docContractTypeDdl.SelectedValue);
                new_doc_cmd.Parameters.AddWithValue("@docOrg", docOrgDdl.SelectedValue);
                new_doc_cmd.Parameters.AddWithValue("@docDept", docDeptDdl.SelectedValue);
                new_doc_cmd.Parameters.AddWithValue("@docDesc", docDescTextBox.Text);
                new_doc_cmd.Parameters.AddWithValue("@PriorContCd", priorContCdTextBox.Text);
                new_doc_cmd.Parameters.AddWithValue("@LegCompContId", legCompContIdTextBox.Text);
                new_doc_cmd.Parameters.AddWithValue("@docUpldDt", DateTime.Now.ToString());
                new_doc_cmd.Parameters.AddWithValue("@docUpldBy", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                new_doc_cmd.Parameters.AddWithValue("@docPath", hdn_doc_path.Value);
                new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value);

                drap_cnxn.Open();
                new_doc_cmd.ExecuteNonQuery();
                //GET THE VALUE OF docIdPk FROM THE INSERTED RECORD
                drap_cnxn.Close();

                if (IsPostBack)
                {
                    //USE THE PRIMARY KEY FROM THE DOCUMENT INSERTED ABOVE FOR docIdFk IN THE FOLLOWING ...

                    SqlCommand new_doc_stat_cmd = new SqlCommand("Insert Into docStats(docStat, docStatDt, docStatSetBy, docIdFk) Values(LTRIM(RTRIM(@docStat)), LTRIM(RTRIM(@docStatDt)), LTRIM(RTRIM(@docStatSetBy)), LTRIM(RTRIM(@docIdFk)))", drap_cnxn);
                    new_doc_stat_cmd.Parameters.AddWithValue("@docStat", "1");
                    new_doc_stat_cmd.Parameters.AddWithValue("@docStatDt", DateTime.Now.ToString());
                    new_doc_stat_cmd.Parameters.AddWithValue("@docStatSetBy", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                    new_doc_stat_cmd.Parameters.AddWithValue("@docIdFk", ??????);

                    drap_cnxn.Open();
                    new_doc_stat_cmd.ExecuteNonQuery();
                    drap_cnxn.Close();                          
                }
            }
        } 
    }

0 个答案:

没有答案