sql server webservice 服务应用及验证

1。在sqlserver 中建立数据库和表,并输入几个数据


2。vs.net 2008中建立类MySqlDB.cs,操作数据库

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;


namespace DBWebService
{
    public class MySqlDB : IDisposable
    {


        public static SqlConnection sqlCon;  //用于连接数据库
        private String ConServerStr = @"Data Source=LYX-PC\;Initial Catalog=StockManage;Integrated Security=True";
        public MySqlDB()
        {
            if (sqlCon == null)
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = ConServerStr;
                sqlCon.Open();
            }
        }
         


        public void Dispose()
        {
            if (sqlCon != null)
            {
                sqlCon.Close();
                sqlCon = null;
            }
        }


       


         public List<string> selectAllCargoInfor()
        {
            List<string> list = new List<string>();


            try
            {
                string sql = "select * from C";
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                SqlDataReader reader = cmd.ExecuteReader();


                while (reader.Read())
                {
                    //将结果集信息添加到返回向量中   
                    list.Add(reader[0].ToString());
                    list.Add(reader[1].ToString());
                    list.Add(reader[2].ToString());


                }


                reader.Close();
                cmd.Dispose();


            }
            catch (Exception)
            {


            }
            return list;
        }


        /// <summary>   
        /// 增加一条货物信息   
        /// </summary>   
        /// <param name="Cname">货物名称</param>   
        /// <param name="Cnum">货物数量</param>   
        public bool insertCargoInfo(string Cname, int Cnum)
        {
            try
            {
                string sql = "insert into C (Cname,Cnum) values ('" + Cname + "'," + Cnum + ")";
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                cmd.ExecuteNonQuery();
                cmd.Dispose();


                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        /// <summary>   
        /// 删除一条货物信息   
        /// </summary>   
        /// <param name="Cno">货物编号</param>   
        public bool deleteCargoInfo(string Cno)
        {
            try
            {
                string sql = "delete from C where Cno=" + Cno;
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                cmd.ExecuteNonQuery();
                cmd.Dispose();


                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


    }
}


3.在Service1.asmx.cs中添加如下程序文件

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;


using System.Collections.Generic;


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


        [WebMethod]
        public String HelloWorld()
        {
            return "Hello World";
        }


        [WebMethod(Description = "获取所有货物的信息")]
        public string[] selectAllCargoInfor()
        {
            return DB.selectAllCargoInfor().ToArray();
        }


        [WebMethod(Description = "增加一条货物信息")]
        public bool insertCargoInfo(string Cname, int Cnum)
        {
            return DB.insertCargoInfo(Cname, Cnum);
        }


        [WebMethod(Description = "删除一条货物信息")]
        public bool deleteCargoInfo(string Cno)
        {
            return DB.deleteCargoInfo(Cno);
        }


    }
}


4。测试


5。继续测试


6。继续测试


看看删除数据库文件效果


ok,已经删除了,能成功操作数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值