调用webService的函数,但是为什么老是提示commandText尚未初始化
我做一个网站系统,想通过在webService.cs里边定义一个函数调用到aspx.cs的代码中,但是为什么老是提示commandText尚未初始化。下面是我的代码:using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.Sql;
using System.Data.Common;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public void ExcuteSql(string strSql)//可以处理无返回的SQL语句
{
//设置连接的字符串:数据库连接的名字为myworkConnectionString,连接的数据源=FF6F878EC62642E,数据库的名称=mywork,ture表示以window身份登录。
string strcon = "Data Source=FF6F878EC62642E;Initial Catalog=mywork;Integrated Security=True";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
//DbProviderFactory看做是一个抽象工厂,生产一个dbProviderFactory对象
DbConnection dbConn = factory.CreateConnection();//设置连接
dbConn.ConnectionString = strcon;
dbConn.Open();
DbCommand command = factory.CreateCommand(); //设置命令
command.Connection = dbConn; //绑定连接
command.CommandText = strSql; //绑定SQL
command.CommandType = CommandType.Text;
command.ExecuteNonQuery(); //执行
dbConn.Close();
dbConn.Dispose();
}
[WebMethod]
public DataTable ExecuteSelect(string strSql)//可以处理select语句,有返回的SQL语句,返回一个DataTable
{
//设置连接并打开
string strcon = "Data Source=FF6F878EC62642E;Initial Catalog=mywork;Integrated Security=True";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
DbConnection dbConn = factory.CreateConnection();//设置连接
dbConn.ConnectionString = strcon;
dbConn.Open();
DbCommand command = factory.CreateCommand(); //设置命令
command.Connection = dbConn; //绑定连接
command.CommandText = strSql; //绑定SQL
command.CommandType = CommandType.Text;
DbDataAdapter adapter = factory.CreateDataAdapter(); //获得一个adapter对象
adapter.SelectCommand = command;
DataSet ds = new DataSet(); //初始化ds
adapter.Fill(ds, "table"); //填充数据到数据表
return ds.Tables["table"]; //返回数据表
}
}
aspx。cs的代码如下:
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.Common;
public partial class 前台_notice : System.Web.UI.Page
{
string strSql;
WebService webService = new WebService();
DataTable dtTable = new DataTable();//声明新表
protected void Page_Load(object sender, EventArgs e)
{
//把notice表的浏览量更新
// strSql = "Update notices set noticeViews=noticeViews+1 where noticeId='" +Request .QueryString ["noticeID"].ToString ()+ "';";
//查询公告的详细消息当
webService.ExcuteSql(strSql);
strSql = "select* form notices where where noticeId='" + Request.QueryString["noticeID"].ToString() + "';";
dtTable = webService.ExecuteSelect(strSql);
Label1.Text = dtTable.Rows[0]["noticeTilte"].ToString();//得到公告的标题
Label2.Text = dtTable.Rows[0]["noticeMatter"].ToString();//得到公告的内容
Label3.Text = dtTable.Rows[0]["noticeViews"].ToString();//得到公告的浏览量
Label4.Text = dtTable.Rows[0]["noticeTime"].ToString();//得到公告的发布时间
}
} --------------------编程问答-------------------- Update Sql 应该对应Adapter.UpdateCommand 或者用 SqlCommandBuidler 创建 --------------------编程问答-------------------- 可是我发下我把 它改成
SqlDataAdapter ad = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
ad.Fill(ds, "table");
return ds.Tables["table"];
con.Close();
仍然是行不通的,一样的问题。而且我只是执行查询strSql = "select* form notices where where noticeId='" + Request.QueryString["noticeID"].ToString() + "';";
而没有执行修改的语句的啊
--------------------编程问答-------------------- 还是提示commandText? 目前看代码没啥问题。。。debug哪行出错了? --------------------编程问答-------------------- ad.Fill(ds, "table");
到这一行就不能运行的了 --------------------编程问答-------------------- 什么错误?CommandText未初始化?
该不是sql文中: "select*" 中间少空格吧。。。 --------------------编程问答-------------------- 还是CommandText未初始化
不是 "select*" 中间少空格的原因 --------------------编程问答-------------------- 是在 WebService 端 Debug 的么?你的错误堆栈贴出来看看。还有现在的WebMethod. --------------------编程问答-------------------- 是在 WebService 端 的Debug ,”还有现在的WebMethod“这个我不太懂诶
这个是:[InvalidOperationException: ExecuteReader: CommandText 属性尚未初始化]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +479
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +86
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +20
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +129
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +10
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +128
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +141
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +91
WebService.ExcuteSql(String strSql) in f:\system\App_Code\WebService.cs:49
前台_notice.Page_Load(Object sender, EventArgs e) in f:\system\qiantai\notice.aspx.cs:28
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
System.Web.UI.Control.OnLoad(EventArgs e) +73
System.Web.UI.Control.LoadRecursive() +52
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2184
--------------------编程问答-------------------- 上面那个不是,这个才是堆跟踪的:
[InvalidOperationException: ExecuteReader: CommandText 属性尚未初始化]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +479
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +86
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +20
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +129
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +10
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +128
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +141
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +91
WebService.ExcuteSql(String strSql) in f:\system\App_Code\WebService.cs:49
qiantai_notice.Page_Load(Object sender, EventArgs e) in f:\system\qiantai\notice.aspx.cs:28
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
System.Web.UI.Control.OnLoad(EventArgs e) +73
System.Web.UI.Control.LoadRecursive() +52
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2184
--------------------编程问答-------------------- 还是没看出来什么问题,不过你这里WebMethod也不是ajax调用,用不着WebService,直接写到一般的类就可以了。
Sql在查询分析器里好使吗?
--------------------编程问答-------------------- ”不过你这里WebMethod也不是ajax调用“和“Sql在查询分析器里好使吗?”这两句话是上面意思的呢,呵呵我挺菜鸟的,不过很感谢您呢
--------------------编程问答-------------------- 1. WebService和asp.net在一个工程里通常是为了前端javascript调用。(ajax)
WebService是一种远程调用的实现方法,比如跨进程,跨机器。。WebService会单独部署到一台服务器。你这里只是给Page后台调用,没有什么意义。你可以直接把[WebMethod],[WebService]都去掉,也能调用。
2. 现在出的问题,从代码上看没看出问题来。
你再把你现在的 WebMethod 的代码贴出来看看。 --------------------编程问答-------------------- WebMethod 这个有代码的吗,我的例子是模仿书上的做的,没有发现WebMethod
您真是有耐心的呢 --------------------编程问答-------------------- //把notice表的浏览量更新
// strSql = "Update notices set noticeViews=noticeViews+1 where noticeId='" +Request .QueryString ["noticeID"].ToString ()+ "';";
//查询公告的详细消息当
webService.ExcuteSql(strSql); ----是不是因为这行没有注释掉
strSql = "select* form notices where where noticeId='" + Request.QueryString["noticeID"].ToString() + "';";
补充:.NET技术 , Web Services