问老师要了个小项目,需要用手机连接数据库,网上查了下最好用webservice我用C#写了个webservice
DBOperation.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 SecurityService
{
public class DBOperation : IDisposable
{
public static SqlConnection sqlCon; //用于连接数据库
//将下面的引号之间的内容换成上面记录下的属性中的连接字符串
private String ConServerStr = @"Data Source=ARRON-PC;Initial Catalog=Security;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> ShowInfo()
{
List<string> list = new List<string>();
try
{
string sql = "select * from Find";
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;
}