1。在sqlserver 中建立数据库和表,并输入几个数据
2。vs.net 2008中建立类MySqlDB.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 DBWebService
{
public class MySqlDB : IDisposable
{
public static SqlConnection sqlCon; //用于连接数据库
private String ConServerStr = @"Data Source=LYX-PC\;Initial Catalog=StockManage;Integrated Security=True";
public MySqlDB()
{
if (sqlCon == null)
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open();
}
}
public void Dispose()
{
if (sqlCon != null)
{
sqlCon.Close();
sqlCon = null;
}
}
public List<string> selectAllCargoInfor()
{
List<string> list = new List<string>();
try
{
string sql = "select * from C";
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;
}
/// <summary>
/// 增加一条货物信息
/// </summary>
/// <param name="Cname">货物名称</param>
/// <param name="Cnum">货物数量</param>
public bool insertCargoInfo(string Cname, int Cnum)
{
try
{
string sql = "insert into C (Cname,Cnum) values ('" + Cname + "'," + Cnum + ")";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 删除一条货物信息
/// </summary>
/// <param name="Cno">货物编号</param>
public bool deleteCargoInfo(string Cno)
{
try
{
string sql = "delete from C where Cno=" + Cno;
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
3.在Service1.asmx.cs中添加如下程序文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
namespace DBWebService
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
MySqlDB DB = new MySqlDB();
[WebMethod]
public String HelloWorld()
{
return "Hello World";
}
[WebMethod(Description = "获取所有货物的信息")]
public string[] selectAllCargoInfor()
{
return DB.selectAllCargoInfor().ToArray();
}
[WebMethod(Description = "增加一条货物信息")]
public bool insertCargoInfo(string Cname, int Cnum)
{
return DB.insertCargoInfo(Cname, Cnum);
}
[WebMethod(Description = "删除一条货物信息")]
public bool deleteCargoInfo(string Cno)
{
return DB.deleteCargoInfo(Cno);
}
}
}
4。测试
5。继续测试
6。继续测试
看看删除数据库文件效果
ok,已经删除了,能成功操作数据库