由于课程答辩,需要连接到数据库,这里使用了SQLServer,但是安卓不好直接连接,需要调用webservice,于是找到了之前大神写的博客,但是由于时间久远,部分版本不对,有些使用不能成功,于是自己在发个自己成功的例子。本人是新手,大神勿喷。。
参考:https://blog.csdn.net/zhyl8157121/article/details/8169172#
先设计数据库,我的数据库名为Android 表为CompetitionInfo,表的设计如下(注意下面,Cno需要设置标识规范):
接下来需要搭建webservice的接口了。
我使用的是Visual Studio 2017 。
点击新建——>项目——>选择ASP.NET.Web应用程序(.NET Framework)
点击确定,并会跳出新建ASP.NET Web 应用程序 ,选择空
打开解决方案资源管理器,右键项目->添加->Web服务(ASMX)
自己设置名字,之后点击确定
创建之后可以看到如下界面
这里已经有自带的HelloWorld()接口,按F5运行,调用此方法会出现如下界面
现在我们要开始写自己调用数据库的方法了。
首先要连接数据库,点击工具->连接到数据库
选择数据选为Microsoft SQL Server (SqlClient),一般默认服务器名为127.0.0.1,并填写要连接的数据库,之后点击测试连接,如若成功则再点击确定。
点击 视图->其他窗口->服务器资源窗口
这里可以看到数据库,并进行相应操作
接下来要编写代码了。先打开解决方案资源管理器,右键项目->添加->类©
自定义名字,之后点击确定
DBOperation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
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 WebserviceTest
{
/// <summary>
/// 一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以
/// </summary>
public class DBOperation : IDisposable
{
public static SqlConnection sqlCon; //用于连接数据库
//将下面的引号之间的内容换成上面记录下的属性中的连接字符串
//Source 为你的数据库地址 Catalog为你的数据库名
private String ConServerStr = @"Data Source=127.0.0.1;Initial Catalog=Android;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;
}
}
//Competiton 表操作
//查询
public List<string> selectAllCompetitonInfo()
{
List<string> list2 = new List<string>();
try
{
string sql = "select * from CompetitionInfo";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//将结果集信息添加到返回向量中
list2.Add(reader[0].ToString());
list2.Add(reader[1].ToString());
list2.Add(reader[2].ToString());
list2.Add(reader[3].ToString());
list2.Add(reader[4].ToString());
list2.Add(reader[5].ToString());
list2.Add(reader[6].ToString());
}
reader.Close();
cmd.Dispose();
}
catch (Exception)
{
}
return list2;
}
//删除
public bool deleteCompetitonInfo(string Cno)
{
try
{
string sql = "delete from CompetitionInfo where Cno=" + Cno;
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
//增加
public bool insertCompetitionInfo(string CTitle, string CStatus, string CHost, string CLevel, string CTime, string CTimng)
{
try
{
string sql = "insert into CompetitionInfo (CTitle,CStatus,CHost,CLevel,CTime,CTimng) values ('" + CTitle + "','" + CStatus + "','" + CHost + "','" + CLevel + "','" + CTime + "','" + CTimng + "')";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
在WebService1.asmx.cs上调用
WebService1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebserviceTest
{
/// <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
{
WebserviceTest.DBOperation dbOperation = new WebserviceTest.DBOperation();
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(Description = "获取所有竞赛信息")]
public string[] selectAllCompeti