当前位置: 代码迷 >> Web Service >> webservice直接连接数据库解决方案
  详细解决方案

webservice直接连接数据库解决方案

热度:268   发布时间:2016-05-02 02:41:26.0
webservice直接连接数据库
从网上找到的都是网站添加web服务引用之后,连接数据库的工作都是网站完成的,有没有webservice直接连接数据库的例子?
------解决方案--------------------
你随便找本稍微正规讲解webservice的应该都有讲解,如果是自己写的webservice的话连接数据库的操作是在webservice中的,你看的可能是编写的网站使用第三方公开的webservice接口,顺便建议,如果你是新接触webservice的话不如学习wcf,呵呵
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

namespace ClothMSN
{
    /// <summary>
    /// ShopService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class ShopService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string[] getNames(string prefixText, int count)
        {
            SqlConnection conn = new SqlConnection("server=.;database=ClothMSN;integrated security=True");
            SqlDataAdapter sda = new SqlDataAdapter("select * from  goodInfo where goodName like '%" + prefixText + "%'", conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            string[] names = null;

            if (dt.Rows.Count > 0)
            {
                names = new string[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    names[i] = dt.Rows[i][1].ToString();
                }
            }
            else
            {
                names = new string[] { "未查找到对应的记录" };
  相关解决方案