一个webservice的项目:
在"生成网站"的时候,提示出错
错误 1 无法将类型“Sup1GetProduct.GetProductResponseGetProductResult”隐式转换为“System.Data.DataTable”
我是一个初学者,请各位不吝赐教,谢谢
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;public class ServiceManager
{
//调用Sup1GetProduct,将商品信息存放到Product1表中
public static void Sup1GetProduct()
{
Sup1GetProduct.Sup1GetProduct myservice = new Sup1GetProduct.Sup1GetProduct();
DataTable dt= myservice.GetProduct();//!!!!!提示myservice处出错
for (int i = 0; i < dt.Rows.Count; i++)
{
string ProId = dt.Rows[i]["ProId"].ToString().Trim();
string ProName = dt.Rows[i]["ProName"].ToString().Trim();
string ProUnit = dt.Rows[i]["ProUnit"].ToString().Trim();
double ProPrice = Convert.ToDouble(dt.Rows[i]["ProPrice"]);
int ProStock = Convert.ToInt16(dt.Rows[i]["ProStock"]);
string ProImage = dt.Rows[i]["ProImage"].ToString().Trim();
string ProSuppId = dt.Rows[i]["ProSuppId"].ToString().Trim();
Database db = DatabaseFactory.CreateDatabase();
string strSql = "insert into product1 values('" + ProId + "','" + ProName + "','" + ProUnit + "'," + ProPrice + "," + ProStock +",'"+ ProImage+"','" + ProSuppId + "')";
DbCommand cmd = db.GetSqlStringCommand(strSql);
try
{
db.ExecuteNonQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
}
}using System;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Web.Services.Protocols;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Sup1GetProduct : System.Web.Services.WebService
{
public Sup1GetProduct () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public DataTable GetProduct()
{
DataSet ds = new DataSet();
string strSql = "select * from Product";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(strSql);
try
{
ds = db.ExecuteDataSet(cmd);
}
catch (Exception ex)
{
throw ex;
}
//DataTable dt = ds.Tables[0];
// return dt;
return ds.Tables[0];
}
}
在"生成网站"的时候,提示出错
错误 1 无法将类型“Sup1GetProduct.GetProductResponseGetProductResult”隐式转换为“System.Data.DataTable”
我是一个初学者,请各位不吝赐教,谢谢
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;public class ServiceManager
{
//调用Sup1GetProduct,将商品信息存放到Product1表中
public static void Sup1GetProduct()
{
Sup1GetProduct.Sup1GetProduct myservice = new Sup1GetProduct.Sup1GetProduct();
DataTable dt= myservice.GetProduct();//!!!!!提示myservice处出错
for (int i = 0; i < dt.Rows.Count; i++)
{
string ProId = dt.Rows[i]["ProId"].ToString().Trim();
string ProName = dt.Rows[i]["ProName"].ToString().Trim();
string ProUnit = dt.Rows[i]["ProUnit"].ToString().Trim();
double ProPrice = Convert.ToDouble(dt.Rows[i]["ProPrice"]);
int ProStock = Convert.ToInt16(dt.Rows[i]["ProStock"]);
string ProImage = dt.Rows[i]["ProImage"].ToString().Trim();
string ProSuppId = dt.Rows[i]["ProSuppId"].ToString().Trim();
Database db = DatabaseFactory.CreateDatabase();
string strSql = "insert into product1 values('" + ProId + "','" + ProName + "','" + ProUnit + "'," + ProPrice + "," + ProStock +",'"+ ProImage+"','" + ProSuppId + "')";
DbCommand cmd = db.GetSqlStringCommand(strSql);
try
{
db.ExecuteNonQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
}
}using System;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Web.Services.Protocols;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Sup1GetProduct : System.Web.Services.WebService
{
public Sup1GetProduct () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public DataTable GetProduct()
{
DataSet ds = new DataSet();
string strSql = "select * from Product";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(strSql);
try
{
ds = db.ExecuteDataSet(cmd);
}
catch (Exception ex)
{
throw ex;
}
//DataTable dt = ds.Tables[0];
// return dt;
return ds.Tables[0];
}
}
另外,个人感觉用直接传递数据库中的数据表的方式来传递数据不是最佳选择
可以考虑设计一个Product对象,将需要传递的数据作为它的属性,通过传递对象的方法来传递数据希望对你有帮助
我将返回值类型改为DataSet,那么在ServiceManager类中获取的时候也改成DataSet.可是还是出错.错误提示变成".....转换为“System.Data.DataSet”"
1、LZ 可以将返回的对象类型 DataTable 改为 object,然后在 Sup1GetProduct 中使用 result as DataTable 进行转换试试。
2、通常是使用 Xml 数据进行传递的,这是出于安全性的考虑,利用 DataTable.WriteXml 生成可 Xml 数据进行传递,然后再 Sup1GetProduct 中反序列化即可。