码迷,mamicode.com
首页 > 数据库 > 详细

用VS2012创建和使用WebService(连接SQL Server)

时间:2015-06-02 20:12:45      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:

1.创建“ASP.NET 空Web应用程序”。打开VS2012,选择“文件”=>“新建”=>“项目”,弹出“新建项目”窗口;然后,选择“Web”=>“ASP.NET空Web应用程序”,可以为新建项目设置“名称”、“位置”、“解决方案名称”,然后点击“确定按钮”,就创建了一个ASP.NET的空Web应用程序。

 

2.打开“服务器资源管理器”,右键单击“数据连接”,选择“添加连接(A)…”,弹出“添加连接”对话框。

技术分享

3.在“添加连接”对话框中,输入“服务器名(E)”、选择“登陆到服务器”的方式,并填入相应信息,然后选择“连接到的数据库”,之后可以点击“测试连接”按钮,测试连接是否成功,然后点击“确定”按钮,添加成功。

技术分享

4.成功添加的连接。

技术分享

5.添加数据库连接。先创建文件夹“DB”,然后在其中添加一个“SQLServerDB.cs”的类,用来连接SQL Server数据库。

技术分享

6.在创建好的SQLServerDB.cs中添加如下代码:

<span style="font-family:SimSun;font-size:18px;">using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace AndroidWebService.DB
{
    /// <summary>
    /// 操作SQL Server数据库的类
    /// </summary>
    public class SQLServerDB
    {
        //连接数据库
        public SqlConnection sqlCon;
        //连接数据库字符串
        public String strSql = @"Data Source=172.17.2.64;Initial Catalog=vjudge;Persist Security Info=True;User ID=sa;Password=1234";

        //默认构造函数
        public SQLServerDB() {
            if (sqlCon == null) {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = strSql;
                sqlCon.Open();
            }
        }

        public void Close() {
            if (sqlCon != null) {
                sqlCon.Close();
                //sqlCon.Dispose();
            }
        }
    }
}</span>


7.添加数据库的操作。先创建文件夹“Action”,然后在其中添加一个“SQLServerAction.cs”的类,用来进行数据库操作。

<span style="font-family:SimSun;font-size:18px;">using AndroidWebService.DB;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace AndroidWebService.Action
{
    public class UserSQLServerAction
    {
        SQLServerDB db = new SQLServerDB();

        /// <summary>
        /// 判断用户登陆
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public string UserLogin( string UserID, string Password ) {
            string strSql = "select NickName from dbo.users where UserID='"+UserID+"' and Password='"+Password+"'";
            SqlCommand sqlCmd = new SqlCommand(strSql, db.sqlCon);
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read()) {
                return sqlReader[0].ToString();
            }
            return "";
        }

        /// <summary>
        /// 返回所有用户列表
        /// </summary>
        /// <returns></returns>
        public List<string> Index()
        {
            List<string> user = new List<string>();
            string strSql = "select * from dbo.users";
            SqlCommand sqlCmd = new SqlCommand(strSql, db.sqlCon);
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            while (sqlReader.Read())
            {
                user.Add( sqlReader[0].ToString() );
                user.Add( sqlReader[1].ToString() );
                user.Add( sqlReader[2].ToString() );
                user.Add( sqlReader[3].ToString() );
            }
            return user;
        }

        /// <summary>
        /// 返回用户详情
        /// </summary>
        /// <returns></returns>
        public List<string> Detail()
        {
            List<string> user = new List<string>();
            string strSql = "select * from dbo.users";
            SqlCommand sqlCmd = new SqlCommand(strSql, db.sqlCon);
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                user.Add(sqlReader[0].ToString());
                user.Add(sqlReader[1].ToString());
                user.Add(sqlReader[2].ToString());
                user.Add(sqlReader[3].ToString());
            }
            return user;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="NickName"></param>
        /// <param name="PhoneNumber"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public string Create( string UserID,string NickName, string PhoneNumber, string Password ) {
            string strSql = "select NickName from dbo.users where UserID='" + UserID + "'";
            SqlCommand sqlCmd = new SqlCommand(strSql, db.sqlCon);
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                return "已存在<" + UserID + ">用户!";             
            }
            sqlReader.Close();
            strSql = "insert into dbo.users values('" + UserID + "','" + NickName + "','" + PhoneNumber + "','" + Password + "')";
            SqlCommand sqlCmdin = new SqlCommand(strSql, db.sqlCon);
            sqlCmdin.ExecuteNonQuery();
            return "添加成功!";
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public string Delete( string UserID )
        {
            string strSql = "select NickName from dbo.users where UserID='" + UserID + "'";
            using (SqlCommand sqlCmd = new SqlCommand(strSql, db.sqlCon))
            {
                SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                if (!sqlReader.Read())
                {
                    return "不存在该用户!";
                }
                sqlReader.Close();
            }
            strSql = "delete from dbo.users where UserID='" + UserID + "'";
            using (SqlCommand sqlCmdin = new SqlCommand(strSql, db.sqlCon))
            {
                sqlCmdin.ExecuteNonQuery();
            }
            return "删除成功!";
        }
    }
}</span>

8.添加Web服务。右键单击项目名称,选择“添加”=>“新建项”,弹出“添加新项”窗口。选择“Web”=>“Web服务”选项,可以为新建项修改“名称”,然后单击“添加”按钮,成功添加了一个以.asmx为后缀的Web Services项。打开新建的Web Service服务,修改代码如下:

<span style="font-family:SimSun;font-size:18px;">using AndroidWebService.Action;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace AndroidWebService
{
    /// <summary>
    /// Summary description for UserSQLServerWebService
    /// </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 UserSQLServerWebService : System.Web.Services.WebService
    {
        UserSQLServerAction uAction = new UserSQLServerAction();

        /// <summary>
        /// 判断登陆
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        [WebMethod]
        public string UserLogin(string UserID, string Password)
        {
            return uAction.UserLogin(UserID,Password);
        }

        /// <summary>
        /// 显示所有用户列表
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string[] Index()
        {
            return uAction.Index().ToArray();
        }

        /// <summary>
        /// 显示某一用户详情
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string[] Detail()
        {
            return uAction.Detail().ToArray();
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="NickName"></param>
        /// <param name="PhoneNumber"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        [WebMethod]
        public string Create(string UserID, string NickName, string PhoneNumber, string Password)
        {
            return uAction.Create( UserID,NickName,PhoneNumber,Password );
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [WebMethod]
        public string Delete(string UserID)
        {
            return uAction.Delete(UserID);
        }
    }
}</span>


用VS2012创建和使用WebService(连接SQL Server)

标签:

原文地址:http://blog.csdn.net/gradonday/article/details/46333767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!