简体   繁体   English

WCF Web服务创建并在Winform应用程序中显示

[英]WCF webservice creation and displaying in winform application

I need a advice and help from wcf experienced professional. 我需要wcf经验丰富的专业人士的建议和帮助。 is it possible to access mySql database of a php website from wcf web service. 是否可以从wcf Web服务访问php网站的mySql数据库。 if yes , can u please show me the way how to create webservice which will have connection string to access database and in response will give data in xml format to my winform application. 如果是,您能告诉我如何创建Web服务的方法,该服务将具有连接字符串以访问数据库,并作为响应将xml格式的数据提供给我的winform应用程序。 And my winform application will consume this data ! 我的winform应用程序将使用此数据! I know basic of web services like adding reference and consuming data from web hosting but never access database from webservice ! 我知道Web服务的基本知识,例如从Web托管添加引用和使用数据,但从不从Webservice访问数据库!

I tested it for Sql database with following code 我使用以下代码针对Sql数据库进行了测试

[WebMethod]
        public string ReturnData()
        {
            SqlConnection con = new SqlConnection();
            try
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SELECT * FROM dataBaseName";
                cmd.CommandType = CommandType.Text;
                if (cmd.ExecuteScalar() == null)
                {
                    return "Database Error !";
                }
                else
                {
                    return cmd.ExecuteScalar().ToString();
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            finally
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
            }

        } 

but when i consume it is showing error .. Please Guide ! 但当我食用它显示错误..请指导!

never access database from webservice 从不访问Web服务中的数据库

If you know rest of the things then this is not a difficult one at all. 如果您了解其他事情,那么这一点都不困难。 just change the connection provider ie OledbConnectionProvider . 只需更改连接提供程序,即OledbConnectionProvider即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM