C# WebService 测试(3)

76 篇文章 1 订阅

在WebService中增加操作数据库的方法。

在Web Service增加数据库操作类:DbAccess.cs

添加引用

using System.Data.SqlClient;

using System.Data;

具体代码如下

namespace WebServiceTest

{

public class DbAccess

{

private static SqlConnection cnn = new SqlConnection();

private static SqlCommand cmd = new SqlCommand();

private static SqlDataAdapter adapter = new SqlDataAdapter();

public SqlTransaction DbTran;

private static string strConn = “Data Source=(local);Initial Catalog=DemoTest;Integrated Security=True”;

private void CreteConn()

{

try

{

if (cnn.State != ConnectionState.Open)

{

cnn.ConnectionString = strConn;

cnn.Open();

}

}

catch (Exception ex)

{

throw ex;

}

}

public void readDataThroughAdapter(string query, DataTable dt)

{

try

{

if (cnn.State == 0)

{

CreteConn();

}

cmd.Connection = cnn;

cmd.CommandText = query;

cmd.CommandType = CommandType.Text;

adapter = new SqlDataAdapter(cmd);

adapter.Fill(dt);

}

catch (Exception ex)

{

throw ex;

}

}

}

在引用服务的App中使用该方法查询数据库中的数,并展示在UI上。

localhost.MyWebService myService = new localhost.MyWebService();

测试代码如下:

private void btnQuery_Click(object sender, RoutedEventArgs e)

{

int id = 0;

if (!int.TryParse(txtId.Text, out id))

{

return;

}

var result = myService.dataTableForUsers(id.ToString());

DataTable dt = JsonConvert.DeserializeObject(result);

dg.ItemsSource = dt.DefaultView;

}

效果如下:

数据库内容如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供关于 C# WebService 服务端的相关信息。C# WebService 服务端是一种基于 SOAP 协议的 Web 服务,它可以通过 HTTP 或 HTTPS 协议进行通信,实现跨平台的数据传输。下面是一个简单的 C# WebService 服务端的实现示例: 1. 创建一个 C# 类,并在类上添加 [WebService] 和 [WebServiceBinding] 属性,示例代码如下: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class MyWebService : System.Web.Services.WebService { // web service methods here } ``` 2. 在类中添加一个或多个 Web 方法,并使用 [WebMethod] 属性标识它们。示例代码如下: ``` [WebMethod] public string HelloWorld(string name) { return "Hello, " + name + "!"; } ``` 3. 在 Web 服务描述语言 (WSDL) 文件中定义 Web 服务的接口和方法。可以使用 Visual Studio 自带的 WSDL 生成工具或手动创建。示例代码如下: ``` <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" targetNamespace="http://tempuri.org/"> <wsdl:message name="HelloWorldSoapIn"> <wsdl:part name="name" type="xsd:string"/> </wsdl:message> <wsdl:message name="HelloWorldSoapOut"> <wsdl:part name="return" type="xsd:string"/> </wsdl:message> <wsdl:portType name="MyWebServiceSoap"> <wsdl:operation name="HelloWorld"> <wsdl:input message="tns:HelloWorldSoapIn"/> <wsdl:output message="tns:HelloWorldSoapOut"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="MyWebServiceSoap" type="tns:MyWebServiceSoap"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="MyWebService"> <wsdl:port name="MyWebServiceSoap" binding="tns:MyWebServiceSoap"> <soap:address location="http://localhost/MyWebService.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ``` 4. 将该类部署到 IIS 或自承载的 ASP.NET 运行时中。在浏览器中访问 Web 服务的 URL(如 http://localhost/MyWebService.asmx),即可看到 Web 服务的描述信息和测试界面。 希望这些信息能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值