简体   繁体   中英

WCF webservice creation and displaying in winform application

I need a advice and help from wcf experienced professional. is it possible to access mySql database of a php website from wcf web service. 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. And my winform application will consume this data ! I know basic of web services like adding reference and consuming data from web hosting but never access database from webservice !

I tested it for Sql database with following code

[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

If you know rest of the things then this is not a difficult one at all. just change the connection provider ie OledbConnectionProvider .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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