SQLServer数据库之用VS2012创建和使用WebService(连接SQL Server)
小标 2018-08-31 来源 : 阅读 1510 评论 0

摘要:本文主要向大家介绍了SQLServer数据库之用VS2012创建和使用WebService(连接SQL Server),通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。

本文主要向大家介绍了SQLServer数据库之用VS2012创建和使用WebService(连接SQL Server),通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。

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中添加如下代码:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace AndroidWebService.DB
{
    /// 


    /// 操作SQL Server数据库的类
    /// 


    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();
            }
        }
    }
}
7.添加数据库的操作。先创建文件夹“Action”,然后在其中添加一个“SQLServerAction.cs”的类,用来进行数据库操作。
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();
        /// 


        /// 判断用户登陆
        /// 

        /// 
        /// 
        /// 
        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 "";
        }
        /// 
        /// 返回所有用户列表
        /// 

        /// 
        public List Index()
        {
            List user = new List();
            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;
        }
        /// 
        /// 返回用户详情
        /// 

        /// 
        public List Detail()
        {
            List user = new List();
            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;
        }
        /// 
        /// 创建用户
        /// 

        /// 
        /// 
        /// 
        /// 
        /// 
        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 "添加成功!";
        }
        /// 
        /// 删除用户
        /// 

        /// 
        /// 
        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 "删除成功!";
        }
    }
}

8.添加Web服务。右键单击项目名称,选择“添加”=>“新建项”,弹出“添加新项”窗口。选择“Web”=>“Web服务”选项,可以为新建项修改“名称”,然后单击“添加”按钮,成功添加了一个以.asmx为后缀的Web Services项。打开新建的Web Service服务,修改代码如下:
using AndroidWebService.Action;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace AndroidWebService
{
    /// 
    /// Summary description for UserSQLServerWebService
    /// 

    [WebService(Namespace = "//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();
        /// 
        /// 判断登陆
        /// 

        /// 
        /// 
        /// 
        [WebMethod]
        public string UserLogin(string UserID, string Password)
        {
            return uAction.UserLogin(UserID,Password);
        }
        /// 
        /// 显示所有用户列表
        /// 

        /// 
        [WebMethod]
        public string[] Index()
        {
            return uAction.Index().ToArray();
        }
        /// 
        /// 显示某一用户详情
        /// 

        /// 
        [WebMethod]
        public string[] Detail()
        {
            return uAction.Detail().ToArray();
        }
        /// 
        /// 创建用户
        /// 

        /// 
        /// 
        /// 
        /// 
        /// 
        [WebMethod]
        public string Create(string UserID, string NickName, string PhoneNumber, string Password)
        {
            return uAction.Create( UserID,NickName,PhoneNumber,Password );
        }
        /// 
        /// 删除用户
        /// 

        /// 
        /// 
        [WebMethod]
        public string Delete(string UserID)
        {
            return uAction.Delete(UserID);
        }
    }
}
  

本文由职坐标整理并发布,希望对同学们学习SQL Server有所帮助,更多内容请关注职坐标数据库SQL Server数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程