Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

WCF Service conect MS SQL database

Îåêßíçóå áðü ôï ìÝëïò dookie. Τελευταία δημοσίευση από το μέλος dookie στις 20-04-2010, 17:28. Υπάρχουν 3 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  20-04-2010, 13:14 58175

    WCF Service conect MS SQL database

    Με αφορμη αυτό το post γνωρίζει κάποιος πως μπορώ απ' το .cs κομμάτι να επικοινωνήσω με μια ms sql βάση?
    Πρέπει να προσθέσω παραμέτρους στο web.config?
    Ευχαριστώ Smile
  •  20-04-2010, 15:57 58191 σε απάντηση της 58175

    Απ: WCF Service conect MS SQL database

    Η ερώτηση σου δεν φαίνεται να έχει καμμία σχέση με WCF. Θα πρέπει να κοιτάξεις πως δουλεύει το ADO.NET - εκτός και αν η ερώτηση σου είναι "Πού αποθηκεύω το connectionstring σε ένα WCF service ?"

     


    Παναγιώτης Καναβός, Freelancer
    Twitter: http://www.twitter.com/pkanavos
  •  20-04-2010, 16:46 58193 σε απάντηση της 58191

    Απ: WCF Service conect MS SQL database

    Για να σου δώσω καλύτερα να καταλάβεις παραθέτω τον κώδικα (με μικρές δοαφορές απ' ότι δίνεται στο παλιό thread)

    HTML Client:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
         <script type="text/javascript">
        
        function CallWcfAjax()
        {
            var xmlHttp = new ActiveXObject("Microsoft.XmlHttp");

            var url = "Service1.svc/ajaxEndpoint/";
            url = url + "Sum2Integers";

            var newResult = "";

            var body = '{"n1":';
            body = body + document.getElementById("num1").value +',"n2":';
            body = body + document.getElementById("num2").value + '}';

             
            // Send the HTTP request
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/json");
            xmlHttp.send(body);

            // Create result handler
            xmlHttp.onreadystatechange = function X() {

                if (xmlHttp.readyState == 4) {
                    var resultsInRaw = xmlHttp.responseText;
                    document.getElementById("results").innerHTML = resultsInRaw;
                }
            }
        }
        </script>

    </head>
    <body>
    First Value:
    <input type="text" id="num1" /> <br>
    Second Value:
    <input type="text" id="num2" /> <br>


    <input type="button" onclick="CallWcfAjax()" value="Call WCF via AJAX" />

    <div id ="results">    </div>


    </body>
    </html>
    _________________________________________________________________________________________________________________

    WCF Service:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;


    namespace Wcf2Ajax
    {
        // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
        public class Service1 : IService1
        {
            public string Sum2Integers(int n1, int n2)

            {
                int result = n1 + n2;
                           string resultStr = result.ToString();
                           // set up the myConnection
                           SqlConnection myConn = new SqlConnection("Data Source=CEID-3A863D11D4;Initial Catalog=SomeTable;Integrated Security=True");
                           SqlCommand cmd = new SqlCommand("INSERT INTO SomeTable values(@firstvalue,@secondvalue)", myConn);
                           cmd.Parameters.Add("@firstvalue", SqlDbType.TinyInt).Value = n1;
                           cmd.Parameters.Add("@secondvalue", SqlDbType.TinyInt).Value = n2;


                           cmd.CommandTimeout = 45;
                           cmd.ResetCommandTimeout();
                           //CommandTimeout is now reset to 30

                           cmd.Connection.Open();
                           cmd.ExecuteNonQuery();
                           cmd.Connection.Close(); 


                return (resultStr);

            }


            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }

            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    }

    Αν και το Service επιστρέφει κανονικά το αποτέλεσμα όταν δεν προσπαθώ να κάνω το insert, όταν προσπαθώ να κάνω insert μου πετάει το μήνυμα:

    Cannot open database "SomeTable" requested by the login. The login failed.
    Login failed for user 'CEID-3A863D11D4\user'.

     Μήπως χρειάζεται να προσθέσω κάποια παράμετρο στο web.config όπως :

    <appSettings>
         <add key="Data Source=CEID-3A863D11D4;Initial Catalog=SomeTable;Integrated Security=True" value="Server=CEID-3A863D11D4; Initial Catalog = SomeTable;uid = sa;pwd=sa"/>
     </appSettings>

    Ελπίζω να έγινα κατανοητός για το τι θέλω να φτιάξω. Smile
  •  20-04-2010, 17:28 58194 σε απάντηση της 58193

    Απ: WCF Service conect MS SQL database

    OK guys fixed.. απλα είχα κάνει λάθος το database name, ήταν ίδιο με του table.Confused




Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems