如何使用jQuery将数据插入SQL数据库

时间:2019-05-15 10:21:47

标签: jquery sql asp.net

如何使用jquery将数据插入SQL数据库。

我正在尝试使用jQuery将数据插入数据库,但是数据没有插入。它显示警告为错误。谁能帮我这个忙。

CS:

[WebMethod]
public static void insertion(string Title,string Description,string  Category,
string Priority,string Attachment)
{
   using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["msgscon"].ConnectionString))
   {
       using (SqlCommand cmd = new SqlCommand("INSERTMD", con))
       {
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@MD_TITLE", Title);
           cmd.Parameters.AddWithValue("@MD_DESC", Description);
           cmd.Parameters.AddWithValue("@MD_CAT_ID", Category);
           cmd.Parameters.AddWithValue("@MD_P_ID", Priority);
           cmd.Parameters.AddWithValue("@MD_ATTACHMENT", Attachment);
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();
       }
   }
}

aspx:

$('#btn_savemd').click(function () {
           var title = $('#tb_title').val();
           var cat = $('#DDL_cat option:selected').index();
           var prior = $('#DDL_prior option:selected').index();
           var desc = $('#tb_desc').val();
           var attach = $('#FileUpload1').val();
           var strfields = ''; var missfields = ''; var i = 0;
           if (title == '') {
               missfields = true;
               i = i + 1;
               strfields += i + " Please enter  Title \n";
           }
           if (cat == 0) {
               missfields = true;
               i = i + 1;
               strfields += i + " Please select Category \n";
           }
           if (prior == 0) {
               missfields = true;
               i = i + 1;
               strfields += i + " Please select Priority \n";
           }
           if (desc == '') {
               missfields = true;
               i = i + 1;
               strfields += i + " Please enter Description \n";
           }

           if (attach == '') {
               missfields = true;
               i = i + 1;
               strfields += i + " Please select Attachment \n";
           }
           if (missfields) {
               alert('Please fill following details \n' + strfields);
               return false;
           }
           var data = { Title: title, Description: desc, Category: cat Priority: prior, Attachment: attach };
           $.ajax({
               type: "POST",
               contentType: "application/json; charset=utf-8",
               url: "jQueryMsgs.aspx/insertion",
               data: '{' + JSON.stringify(data) + '}',
               dataType: "json",
               success: function () {
                       alert('Message Details Added Successfully');
               },
               error: function () {
                   alert('Error');
               }
           });
       });

0 个答案:

没有答案