关于从MySQL数据库检索数据的Web服务

时间:2015-11-25 15:40:23

标签: mysql web-services

我正在尝试创建一个可以从MYSQL数据库获取数据的Web服务,但我遇到了问题

网络表单代码

namespace John
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindGrid();
            }
        }

        private void BindGrid()
        {
           localhost.Service1 service = new localhost.Service1();
            GridView1.DataSource = service.Get(); 
            GridView1.DataBind();
        }
    }
}

和网络服务代码

public DataTable Get()
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM nursery1"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            dt.TableName = "nursery1";
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
            }

我只是按照本教程中的步骤进行操作:

http://www.aspsnippets.com/Articles/Retrieve-Get-data-from-Database-using-Web-Service-in-ASPNet-using-C-and-VBNet.aspx

或者如果您有任何关于在ASP.NET和C#中使用Web服务从SQL数据库获取数据的有用参考,请帮助我

提前致谢

0 个答案:

没有答案
相关问题