C# 使用C在Asp.net中使用web服务将数据插入数据库#
我想知道是否可以请您帮助我使用C#在Asp.net中使用web服务将数据插入数据库。 我通过MySql工作台创建了数据库,然后创建了Web服务,之后我尝试在这个链接中应用教程 ,但它对我不起作用,这是正在使用的代码:C# 使用C在Asp.net中使用web服务将数据插入数据库#,c#,mysql,asp.net,web-services,gridview,C#,Mysql,Asp.net,Web Services,Gridview,我想知道是否可以请您帮助我使用C#在Asp.net中使用web服务将数据插入数据库。 我通过MySql工作台创建了数据库,然后创建了Web服务,之后我尝试在这个链接中应用教程 ,但它对我不起作用,这是正在使用的代码: using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.Configuration; using System.Data.SqlClient; u
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Xml;
namespace toleen
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public DataTable Get()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM nursery"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
dt.TableName = "nursery";
sda.Fill(dt);
return dt;
}
}
}
}
}
[WebMethod]
public void Insert(int n_id, string n_name, string n_address)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO nursery(n_id, n_name, n_address) VALUES (@n_id, @n_name, @n_address)"))
{
cmd.Parameters.AddWithValue("@n_id", n_id);
cmd.Parameters.AddWithValue("@n_name", n_name);
cmd.Parameters.AddWithValue("@n_address", n_address);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
[WebMethod]
public void myThrow()
{
// Build the detail element of the SOAP fault.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element,
SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);
// Build specific details for the SoapException.
// Add first child of detail XML element.
System.Xml.XmlNode details =
doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1",
"http://tempuri.org/");
System.Xml.XmlNode detailsChild =
doc.CreateNode(XmlNodeType.Element, "childOfSpecialInfo",
"http://tempuri.org/");
details.AppendChild(detailsChild);
// Add second child of detail XML element with an attribute.
System.Xml.XmlNode details2 =
doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2",
"http://tempuri.org/");
XmlAttribute attr = doc.CreateAttribute("t", "attrName",
"http://tempuri.org/");
attr.Value = "attrValue";
details2.Attributes.Append(attr);
// Append the two child elements to the detail node.
node.AppendChild(details);
node.AppendChild(details2);
//Throw the exception
SoapException se = new SoapException("Fault occurred",
SoapException.ClientFaultCode,
Context.Request.Url.AbsoluteUri,
node);
throw se;
return;
}
}
}
但是当我进行stat调试时,这个问题发生在以下行:GridView1.DataSource=service.Get()代码>
用户代码中未处理Soap异常
你能帮我解决这个问题吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace toleen
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
CRUD_Service.Service1 service = new CRUD_Service.Service1();
GridView1.DataSource = service.Get();
GridView1.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
CRUD_Service.Service1 service = new CRUD_Service.Service1();
service.Insert(Convert.ToInt32(txtId.Text.Trim()), txtName.Text.Trim(), txtaddress.Text.Trim());
this.BindGrid();
}
}
}