插入语句插入2条记录而不是1条

时间:2017-07-31 09:54:32

标签: c# asp.net sql-server

我使用简单的sql语句插入记录,一次只输入1条记录,但是插入2次 这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
    { 
       //First Query

        SqlConnection con = new SqlConnection(code.connection);
        con.Open();
        String query = "insert into issue values(@pro_id,@impect,@type,@summary,@detail,@attach,@comment,@mem_id,@priority,@status,getdate(), @cust_name,@mail,'','','') select SCOPE_IDENTITY() ";
        SqlCommand com = new SqlCommand(query, con);
        com.Parameters.AddWithValue("@pro_id", "PB");
        com.Parameters.AddWithValue("@impect", impect);
        com.Parameters.AddWithValue("@type", IssueList.SelectedItem.ToString());
        com.Parameters.AddWithValue("@summary", summary.Text);
        com.Parameters.AddWithValue("@detail",detail1.Text);
            com.Parameters.AddWithValue("@attach", "");
        com.Parameters.AddWithValue("@comment", Comments.Text);
        com.Parameters.AddWithValue("@mem_id", "");
        com.Parameters.AddWithValue("@priority", "");
        com.Parameters.AddWithValue("@status", "Submitted");
        com.Parameters.AddWithValue("@cust_name", "ramisha");//to be add auto
        com.Parameters.AddWithValue("@mail", "rami@gmail.com");// to be add auto
        if (com.ExecuteNonQuery() >= 1)
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Request issue Submitted Successfully!');</script>");
            SqlDataReader sdr = com.ExecuteReader();
            if (sdr.Read())
            {
                issue_id = int.Parse(sdr[0].ToString());
                Response.Write("<script LANGUAGE='JavaScript' >alert(issue_id);</script>");
            }
            sdr.Close();
        }
        con.Close();

       // 2nd query

        SqlConnection con2 = new SqlConnection(code.connection);
        con2.Open();
        String query2 = "insert into contact values(@id,@cust_name,@mail,@ph_no,@time)";
        SqlCommand com2 = new SqlCommand(query2, con2);
        com2.Parameters.AddWithValue("@id", issue_id);
        com2.Parameters.AddWithValue("@cust_name", contactN);
        com2.Parameters.AddWithValue("@mail", contactM);
        if (itsOkToCall.Checked == true)
        {
            com2.Parameters.AddWithValue("@ph_no", phone.Text);
            com2.Parameters.AddWithValue("@time", FromDrop.SelectedItem.ToString() + " to " + toDrop.SelectedItem.ToString());
        }
        else
        {
            com2.Parameters.AddWithValue("@ph_no", "");
            com2.Parameters.AddWithValue("@time", "");
        }
        if (com2.ExecuteNonQuery() >= 1)
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!');window.location='Default.aspx';</script>");
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Request not Submitted Successfully!');window.location='Default.aspx';</script>");
        }

        con2.Close();

这里第一个查询是插入2个记录,而假设只输入1个记录。第二是插入1条记录,这是完美的。现在有人帮我解决问题

0 个答案:

没有答案