110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace WebSite6
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://www.baidu.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
static MySqlConnection getConnection()
{
//this static method will return a connection constructed by connection string
string myConnectionString = "Database=jw303;Data Source=achilles.mcscw3.le.ac.uk ;User Id=jw303;Password=ousetypt"; MySqlConnection connection = new MySqlConnection(myConnectionString);
return connection;
}
static void Main(string[] args)
{
Service1 app = new Service1();
Console.WriteLine(app.getBooksByTitle("Ajax_on_Java"));
Console.ReadLine();
}
[WebMethod]
public Books getBooksByTitle(string title)
{
Books b = null;
try
{
MySqlConnection connection = getConnection();
connection.Open();
MySqlCommand mycm = new MySqlCommand("", connection);
mycm.Prepare();
mycm.CommandText = String.Format("select Title,Copy,On_Loan from Copy_of_Books where Title=?Title_para");
mycm.Parameters.AddWithValue("?Title_para", title);
MySqlDataReader msdr = mycm.ExecuteReader();
while (msdr.Read())
{
if (msdr.HasRows)
{
b = new Books();
b.Title = msdr.GetString("title");
b.Copy = msdr.GetString("copy");
b.On_Loan = msdr.GetString("on_loan");
}
}
msdr.Close();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return b;
}
[WebMethod]
public Books getBooksByAuthor(string author)
{
Books b = null;
try
{
MySqlConnection connection = getConnection();
connection.Open();
MySqlCommand mycm = new MySqlCommand("", connection);
mycm.Prepare();
mycm.CommandText = String.Format("select AuthorName,Title,Copy,On_Loan from Author, Copy_of_Books where Author.AuthorID=Copy_of_Books.Auth and AuthorName=?Auth_para");
mycm.Parameters.AddWithValue("?Auth_para", author);
MySqlDataReader msdr = mycm.ExecuteReader();
while (msdr.Read())
{
if (msdr.HasRows)
{
b = new Books();
b.Title = msdr.GetString("author");
b.Title = msdr.GetString("title");
b.Copy = msdr.GetString("copy");
b.On_Loan = msdr.GetString("on_loan");
}
}
msdr.Close();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return b;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
namespace WebSite6
{
public class Books
{
private string bookno; //2
public string BookNo
{
get { return bookno; }
set { bookno = value; }
}
private string title; //2
public string Title
{
get { return title; }
set { title = value; }
}
private string copy; //3
public string Copy
{
get { return copy; }
set { copy = value; }
}
private string on_loan; //4
public string On_Loan
{
get { return on_loan; }
set { on_loan = value; }
}
private double author; //5
public double AuthorName
{
get { return author; }
set { author = value; }
}
public override string ToString()
{
return "The Book is :" + this.bookno + "\tName:" + this.title + "\t" + this.copy + "\n\tOn_Loan:" + this.on_loan + "\t AuthorName:" + this.author;
}
}
}