gpt4 book ai didi

c# - 在Asp.net C#中使用jQuery Ajax插入数据[数据库MySQL]

转载 作者:行者123 更新时间:2023-11-29 18:39:07 25 4
gpt4 key购买 nike

我需要在 Asp.net C# 中使用 jQuery Ajax 在 MySql 数据库中插入新记录。

我在使用WebService方法时尝试过这个教程: https://codepedia.info/insert-data-using-jquery-ajax-in-asp-net-csharp-database-ms-sql-server/

我没有错误,但数据未插入。

我尝试插入简单的查询sql,但没有成功。

你能帮我吗?

提前谢谢您。

我的代码简化如下。

WebService.cs

using System;
using System.Configuration;
using System.Data.Odbc;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class WebService : System.Web.Services.WebService
{
public WebService()
{

}

public class userDetails
{
public string firstName;
}

[WebMethod]
public void AddRecord(userDetails userDetails)
{
//SIMPLE SQL QUERY INSERT INTO
string query = String.Format(" INSERT INTO doTable ");
query += String.Format(" (name) ");
query += String.Format(" VALUES ('foo'); ");

using (OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(query, conn))
{
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
cmd.Connection.Close();
}
}
}
}
}

HMTL页面:

<script src="3.2.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function () {

$("#activatedLink").on('click', function (e) {

e.preventDefault();
var userDetails = {};

userDetails.firstName = $("#randomdirectory").val();

var jsonData = JSON.stringify({
userDetails: userDetails
});

$.ajax({
type: "POST",
url: "WebService.asmx/AddRecord",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnErrorCall
});

function OnSuccess(response) {
var result = response.d;
if (result == "success") {
$("#msg").html("New record addded successfully :)").css("color", "green");
}
$("#randomdirectory").val("");
}

function OnErrorCall(response) {
$("#msg").html("Error occurs :(").css("color", "red");
}

});

});

</script>



<form id="form1">
<div id="Tree">
<input type="hidden" id="randomdirectory" name="randomdirectory" value="CEF">
<a id="activatedLink" data-id="CEF"
href="javascript:document.getElementById('loading').style.visibility = 'visible';
location.href='http://...';" target="_top">
<span>CEF</span></a>
</div>
</form>

enter image description here

最佳答案

you can also create a static web method at back end of aspx page and can call back end method and insert data
use below code :

write web method at back end in aspx.cs file:
[WebMethod]

public static void AddRecord(userDetails userDetails)
{
//SIMPLE SQL QUERY INSERT INTO
string query = String.Format(" INSERT INTO doTable ");
query += String.Format(" (name) ");
query += String.Format(" VALUES ('foo'); ");

using (OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(query, conn))
{
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
cmd.Connection.Close();
}
}
}
}




<script src="3.2.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function () {

$("#activatedLink").on('click', function (e) {

e.preventDefault();
var userDetails = {};

userDetails.firstName = $("#randomdirectory").val();

var jsonData = JSON.stringify({
userDetails: userDetails
});

$.ajax({
type: "POST",
url: "pagename.aspx/AddRecord", //here specify the name of our page where the method is present
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnErrorCall
});

function OnSuccess(response) {
var result = response.d;
if (result == "success") {
$("#msg").html("New record addded successfully :)").css("color", "green");
}
$("#randomdirectory").val("");
}

function OnErrorCall(response) {
$("#msg").html("Error occurs :(").css("color", "red");
}

});

});

</script>



<form id="form1">
<div id="Tree">
<input type="hidden" id="randomdirectory" name="randomdirectory" value="CEF">
<a id="activatedLink" data-id="CEF"
href="javascript:document.getElementById('loading').style.visibility = 'visible';
location.href='http://...';" target="_top">
<span>CEF</span></a>
</div>
</form>

关于c# - 在Asp.net C#中使用jQuery Ajax插入数据[数据库MySQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com