今天试了试返回dataset的webservice

    技术2022-05-11  16

    using  System; using  System.Collections; using  System.ComponentModel; using  System.Data; using  System.Diagnostics; using  System.Web; using  System.Web.Services; using  System.Data.OleDb; namespace  dbService {    /// <summary>    /// Service1 的摘要说明。    /// </summary>    public class dbService : System.Web.Services.WebService    {        private String Connstr;        public dbService()        {            //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的                                    InitializeComponent();            Connstr="Provider=SQLOLEDB;Data Source=(local);";            Connstr=Connstr+"Initial catalog=PUBS;User ID=sa;PassWord=sa";        }        组件设计器生成的代码        // WEB 服务示例        // HelloWorld() 示例服务返回字符串 Hello World        // 若要生成,请取消注释下列行,然后保存并生成项目        // 若要测试此 Web 服务,请按 F5 键        [WebMethod]        public DataSet populate(String SQL)        {                OleDbConnection myConnection=new  OleDbConnection(Connstr);            OleDbDataAdapter myCommand=new OleDbDataAdapter(SQL,myConnection);            DataSet ds=new DataSet();            myCommand.Fill(ds,"vTable");            return ds;        }        [WebMethod]        public String RunSQL(String vsql)        {            try            {                OleDbConnection myConnection =new OleDbConnection(Connstr);                OleDbCommand mycommand=new OleDbCommand(vsql,myConnection);                myConnection.Open();                mycommand.ExecuteNonQuery();                myConnection.Close();                return("OK");            }            catch (System.Exception e)            {                String ret="出现异常"+  e.ToString();                return ret;                            }        }            //        public string HelloWorld()        //        {        //            return "Hello World";        //        }    }}

     以上是webservice的代码

     

    using  System; using  System.Collections; using  System.ComponentModel; using  System.Data; using  System.Drawing; using  System.Web; using  System.Web.SessionState; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Web.UI.HtmlControls; namespace  TheClient1 {    /// <summary>    /// WebForm1 的摘要说明。    /// </summary>    public class WebForm1 : System.Web.UI.Page    {        protected System.Web.UI.WebControls.DropDownList DropDownList1;        protected System.Web.UI.WebControls.DataGrid DataGrid1;        protected System.Web.UI.WebControls.Label Label1;        protected System.Web.UI.WebControls.Button RunSql;        protected System.Web.UI.WebControls.Button Populate;        protected System.Web.UI.WebControls.Label message;        private localhost.dbService mydbService=new localhost.dbService();            private void Page_Load(object sender, System.EventArgs e)        {            // 在此处放置用户代码以初始化页面        }        Web 窗体设计器生成的代码        private void Populate_Click(object sender, System.EventArgs e)        {            DataSet ds=new  DataSet();            String s=DropDownList1.SelectedItem.ToString();            ds=mydbService.populate(s);            DataGrid1.DataSource=ds;            DataGrid1.DataBind();        }        private void RunSql_Click(object sender, System.EventArgs e)        {            String s;            String ret;            s="insert into pub_info(pub_id,pr_info) values('23','asdfsd')";            ret=mydbService.RunSQL(s);            message.Text=ret;            DataSet ds=new DataSet();            s="Select * from pub_info where pub_id='9999'";            ds=mydbService.populate(s);            DataGrid1.DataSource=ds;            DataGrid1.DataBind();        }    }} 以上是引用webservice的web页面的代码

    最新回复(0)