vs2010开发c#WebService访问SqlServer数据库
2018-09-14 15:08阅读:
一 开发环境
操作系统:Win7 64位
数据库:Sql Server2008 R2
服务器端:VS2010
配置IIS服务
IE 浏览器
二 数据库设计
创建数据库BOOK,
表 user,
字段和内容 如下:
id
name
age
1 李芳
18
2 王苗
20
3 王永
23
三 服务器端程序(C# WebService)
使用VS2010 编写服务端的C# WebService 提供服务,供其它应用访问。
(一). 新建一个Webservice项目
文件--新建--项目
模板选择 Visual C# -- Web , 具体选择 ASP.NET 空 Web 应用程序
名称默认:WebApplication1
确定
(二) 在VS中连接数据库
这一步主要是方便在VS中查看数据库的内容,
如果你非常熟悉SQL数据库,本步骤可以不做
1. 菜单 视图 -- 服务器资源管理器 --
右击 数据连接 -> 添加连接

2. 配置数据连接
(1). 点击 数据源 后面的更改
选择Microsoft Sqlserver,返回
(2). 点击 服务器 后面的刷新
从中选择或输入自己的计算机名称
(3). 登录服务器 两种方式 均可
(4). 选择 或输入一个数据名,选刚建的 book
(5). 点击 测试连接 直到 检测连接成功,点击确定返回

到此就可以在VS中查看和编辑数据库
3. 查到连接的字符串
先点击 服务器资源管理器 中的
刚建立的连接 dell-pc-15.BOOK.dbo
转到
解决方案资源管理器,找到连接字符串如下:
Data Source=DELL-PC-15;Initial Catalog=BOOK;Integrated
Security=True
(三) 在VS中相关代码
1 创建 操作数据库的类
右击 解决方案资源管理器中的 WebApplication1 -- 添加 -- 新建项
在画面中选择
类, 名称输入 SQLhelper.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.RegularExp ressions;
using System.Collections;
using System.Collections.Generic;
namespace WebApplication1
{
/// <summary>
///
一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以
/// </summary>
public class SQLhelper : IDisposable
{
public static SqlConnection sqlCon;
//用于连接数据库
//将下面的引号之间的内容换成上面记录下的属性中的连接字符串dell-PC-15或IP地址
private String ConServerStr =
@'Data Source=10.3.1.37;Initial Catalog=book;Integrated
Security=True';
//默认构造函数
public DBOperation()
{
if (sqlCon ==
null)
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open();
}
}
//关闭/销毁函数,相当于Close()
public void Dispose()
{
if (sqlCon !=
null)
{
sqlCon.Close();
sqlCon = null;
}
}
/// <summary>
/// 获取所有货物的信息
/// </summary>
/// <returns>所有货物信息</returns>
public List<string>
selectAllUser()
{
List<string> list =
new List<string>();
try
{
string sql = 'select * from user';
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;
}
}
}
2
Service1.asmx.cs如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MyProject;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </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 WebService1 :
System.Web.Services.WebService
{
DBOperation dbOperation = new
DBOperation();
[WebMethod]
public string HelloWorld()
{
return 'Hello
World';
}
[WebMethod(Description =
'查询所有用户')]
public string[]
selectAllUser()
{
return
SQLhelper.selectAllUser().ToArray();
}
}
}
3 菜单
生成 -- 生成
WebApplication1
直到没有错误
4 在VS2010中 运行 程序
按F5键执行,转到 浏览器
http://localhost:19143/
点击 清单中的 WebService1.asmx
转到 http://localhost:19143/WebService1.asmx
页面列出内容如下:
支持下列操作。有关正式定义,请查看服务说明。
HelloWorld
selectAllUser
查看所有用户

5 点击 selectAllUser
转到 浏览器
http://localhost:19143/WebService1.asmx?op=selectAllUser

6 点击 调用
转到 浏览器 http://localhost:19143/WebService1.asmx/selectAllUser
This XML file does not appear to have any style information
associated with it. The document tree is shown below.
<ArrayOfString
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://tempuri.org/'>
<string>1</string>
<string>李芳</string>
<string>18</string>
<string>2</string>
<string>王苗</string>
<string>20</string>
<string>3</string>
<string>王永</string>
<string>30</string>
</ArrayOfString>
到此 WebService 连接 Sql Server数据库成功

7 发布程序
菜单
生成 -- 发布
WebApplication1
发布方法 选择 文件系统
目标位置 选择一个自己的文件夹 比如 d:\MyWebService
点击 发布 ,直到提示发布成功即可
四 网站部署
1
IIS设置
控制面板--程序和功能--打开或关闭Windows功