找不到类型或命名空间名称,是不是版本问题(某些代码限制了)

changeking 2009-11-24 06:36:24
我备份了下面这两个文件,然后回到家新建了一个项目,再把这两个文件复制或者添加进去,为什么就运行不了呢??出现找不到类型或命名空间名称“Books”
Service1.asmx.cs

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;
}
}
}


下面是Books.cs

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;
}
}
}
...全文
595 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
acqy 2009-11-25
  • 打赏
  • 举报
回复
你两个项目,假设Service1在项目Project1下,而Books在项目Project2下,那么你就在Project1上点右键,选择“添加引用”,然后在弹出的对话框中,选择“项目”(或者“工程”,忘记了)选项卡,之后在列表中选择Project2后确定,就可以了。

你的问题其实很简单,根据楼上的答复,真不知为何需要添加Web引用。
changeking 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wartim 的回复:]
添加引用->项目->选择book.cs的那个项目

webservice的命名空间写成百度。。。真强大
[/Quote]
添加引用->项目->没东西可以选
我点浏览也没办法添加工程..
cuike519 2009-11-24
  • 打赏
  • 举报
回复
编译了么?
wartim 2009-11-24
  • 打赏
  • 举报
回复
添加引用->项目->选择book.cs的那个项目

webservice的命名空间写成百度。。。真强大
changeking 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acqy 的回复:]
我估计你的Books和Service1两个类被创建在了两个不同的项目中。如果是这样的话,你需要在Service1所在的项目上添加对Books项目的引用。
[/Quote]
请教一下怎样添加对Books项目的引用啊?
lzsh0622 2009-11-24
  • 打赏
  • 举报
回复
添加你的WebServices引用
acqy 2009-11-24
  • 打赏
  • 举报
回复
我估计你的Books和Service1两个类被创建在了两个不同的项目中。如果是这样的话,你需要在Service1所在的项目上添加对Books项目的引用。
changeking 2009-11-24
  • 打赏
  • 举报
回复
两个文件都保存在同一个namespace下为什么还是不行啊??高手来帮帮忙

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧