从表中收集数据并通过链接作为查询字符串传递

时间:2016-04-06 11:00:28

标签: c# sql-server query-string

广告"广告"将存储下面控制器中的信息的表格,该表格具有" AdsID"生成每行自动编号的列。所以我要找的是在表格中插入信息表单控制器后,获取按钮点击时刚创建的行的AdsID号码并传递行号" AdsID"和" AdsTit"通过EditAds.aspx

protected void adNewQdadsbtn_Click(object sender, EventArgs e)
    {
       SqlConnection cn = new SqlConnection(sc);

                if (RegInteFileUploadImg1.HasFile || RegInteFileUploadImg2.HasFile || RegInteFileUploadImg3.HasFile || RegInteFileUploadImg4.HasFile || RegInteFileUploadImg5.HasFile)
                {


                    var files = new[] { FileExtentio, FileExtentio2, FileExtentio3, FileExtentio4, FileExtentio5 };
                    files = files.Where(s => !string.IsNullOrEmpty(s)).ToArray();
                    var extensions = new[] { ".jpg", ".png" };
                    if ((files.Except(extensions).Count()) <= 0)
                    {
                        if (RegDropDownList3.SelectedValue == "no")
                        {
                            AdsWrngPanel.Visible = true;
                            adsstutslbel.Text = "- Please select ads status";
                        }
                        else
                        {

                        }


                        if (DropDownList11.SelectedValue == "no")
                        {
                            AdsWrngPanel.Visible = true;
                            adscondlbel.Text = "- Please select ads condition";
                        }


                        string sqlstatment = @"INSERT INTO ads (Section, Category, AdsTit, AdsDesc, Country, State,AdsDate,City, AdsPrice, Img1, img2, img3, img4,img5, Wtags, QAdsTeleNum,Condition, Status,Maker,Year,Gear,RoomNo, Space,Shower,Garage,Currency,extlink,AdType,approvAds,QAdsEmail)
        VALUES (@Section, @Category, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Img1, @img2, @img3,
        @img4, @img5, @Wtags, @QAdsTeleNum,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage,@Currency, @extlink,@AdType,@approvAds,@QAdsEmail)";

                        SqlCommand cmd = new SqlCommand(sqlstatment, cn);


                        String shw = "Y";
                        cmd.Parameters.AddWithValue("@Section", RegSecdrdoads.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@Maker", makervalue);
                        cmd.Parameters.AddWithValue("@Year", Yearvalue);
                        cmd.Parameters.AddWithValue("@Gear", Gearvalue);
                        cmd.Parameters.AddWithValue("@RoomNo", RoomNum);
                        cmd.Parameters.AddWithValue("@Space", areasize);
                        cmd.Parameters.AddWithValue("@Category", RegCatedrdoads.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
                        cmd.Parameters.AddWithValue("@AdsDesc", NyAdsDesc);
                        cmd.Parameters.AddWithValue("@Country", cookie.Value);


                        string imgnouser = "/images/general/nouser.jpg";

                        if (RegInteFileUploadImg1.HasFile)
                        {
                            cmd.Parameters.AddWithValue("@Img1", RegInteFileUploadImg1.FileName);
                            RegInteFileUploadImg1.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg1.FileName));
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@Img1", imgnouser);
                        }


                        if (RegInteFileUploadImg2.HasFile)
                        {
                            cmd.Parameters.AddWithValue("@Img2", RegInteFileUploadImg2.FileName);
                            RegInteFileUploadImg2.SaveAs(Server.MapPath("~/images/AdsImgs/" + RegInteFileUploadImg2.FileName));
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@Img2", imgnouser);
                        }

                        cn.Open();
                        cmd.ExecuteNonQuery();
                        Response.Redirect("User panel.aspx");

                    }

                    else
                    {
                        AddNwAddsWrngFrmtLbl.Text = "Error: The file should have .png or .jpg format only";
                        AddNwAddsWrngFrmtLbl.ForeColor = System.Drawing.Color.Red;

                        return;
                    }

                }

                else
                {
                    string sqlstatment2 = @"INSERT INTO ads (Section, Category, AdsTit, AdsDesc, Country, State,AdsDate,City, AdsPrice, Wtags, QAdsTeleNum,Condition, Status,Maker,Year,Gear,RoomNo, Space,Shower,Garage,extlink,AdType,approvAds,QAdsEmail) VALUES (@Section, @Category, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Wtags, @QAdsTeleNum,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage, @extlink,@AdType,@approvAds,@QAdsEmail)";


                    SqlCommand cmd = new SqlCommand(sqlstatment2, cn);

                    String SHO = "Y";
                    cmd.Parameters.AddWithValue("@Section", RegSecdrdoads.SelectedItem.Text);
                    cmd.Parameters.AddWithValue("@Maker", makervalue);
                    cmd.Parameters.AddWithValue("@Year", Yearvalue);
                    cmd.Parameters.AddWithValue("@Gear", Gearvalue);
                    cmd.Parameters.AddWithValue("@RoomNo", RoomNum);
                    cmd.Parameters.AddWithValue("@Space", areasize);
                    cmd.Parameters.AddWithValue("@Category", RegCatedrdoads.SelectedItem.Text);
                    cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
                    cmd.Parameters.AddWithValue("@AdsDesc", NyAdsDesc);
                    cmd.Parameters.AddWithValue("@adsDate", DateTime.Now);
                    cmd.Parameters.AddWithValue("@Currency", AddAdsCurencyLBL.Text);
                    cmd.Parameters.AddWithValue("@extlink", extlinktxtbox.Text);
                    cmd.Parameters.AddWithValue("@AdType", QUAD);
                    cmd.Parameters.AddWithValue("@approvAds", AdsAprov);
                    cmd.Parameters.AddWithValue("@QAdsEmail", QuickAdsEmailTxtBox.Text);


                    cn.Open();
                    cmd.ExecuteNonQuery();

                }

                string QAdsID = string.Empty;
                string QADSTit = string.Empty;


                using (SqlCommand SendMailcmd = new SqlCommand("SELECT AdsID FROM ads WHERE QAdsEmail = @QAdsEmail AND AdsTit=@AdsTit OR QAdsTeleNum=@QAdsTeleNum"))
                {
                    SendMailcmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text.Trim());
                    SendMailcmd.Parameters.AddWithValue("@QAdsEmail", QuickAdsEmailTxtBox.Text.Trim());
                    SendMailcmd.Parameters.AddWithValue("@QAdsTeleNum", QuickAdsMobNumTxtBox.Text.Trim());
                    SendMailcmd.Connection = cn;

                    using (SqlDataReader sdr = SendMailcmd.ExecuteReader())
                    {
                        if (sdr.Read())
                        {
                            QAdsID = sdr["AdsID"].ToString();
                            QADSTit = sdr["AdsTit"].ToString();
                        }
                    }
                    cn.Close();
                }

                Response.Redirect("EditAds.aspx?Name=" + this.addadstittxtbx.Text );


    }

1 个答案:

答案 0 :(得分:1)

如果我确实理解了您的问题,那么您是否尝试将插入的行的ID返回给您的应用程序?

如果是,请考虑以下事项。由于您似乎插入了单行,并假设您使用的是SqlServer 2005或更高版本,因此它非常简单。

您可以将OUTPUT clause与insert语句一起使用,并立即返回每行的ID。如果你构建你的查询,如

INSERT INTO table (<column>)
    OUTPUT inserted.<column>
    VALUES (<@parameter>) 

您只需使用cmd.ExecuteScalar()cmd.ExecuteReader()即可获取新插入的ID。

/ edit:尝试在代码中实现它(省略ParameterValues,因为它保持不变)

// initialization stuff left out because it stays the same [...]
string sqlstatment = @"INSERT INTO ads (Section, Category, AdsTit, AdsDesc, Country, State,AdsDate,City, AdsPrice, Img1, img2, img3, img4,img5, Wtags, QAdsTeleNum,Condition, Status,Maker,Year,Gear,RoomNo, Space,Shower,Garage,Currency,extlink,AdType,approvAds,QAdsEmail) 
    OUTPUT inserted.AdsID        
    VALUES (@Section, @Category, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Img1, @img2, @img3,
    @img4, @img5, @Wtags, @QAdsTeleNum,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage,@Currency, @extlink,@AdType,@approvAds,@QAdsEmail)";
// ommitting parameter stuff [...]

cn.Open();
string QAdsID = cmd.ExecuteScalar() as string; // cast or convert the proper data type here...

您当然可以输出多个插入的值,但是您需要DataReaderDataAdapter而不是ExecuteScalar()