簡體   English   中英

Web服務 - Web方法和數據庫

[英]Web Service - Web Method and Database

我正在嘗試編寫一個Web方法,使我能夠通過Web服務直接將文本INSERT到sql表。 我設法在Web服務xml頁面上查看手動輸入到表的任何文本。 我現在想要做的是能夠將數據從Web服務插入到數據庫中。

這背后的想法是,用戶能夠訪問視圖並通過Web服務向數據庫添加信息/文本。 我將非常感謝在下面的WebMethod(public void InsertInfo ...)中解決問題的方向。

我想幫助如何創建/添加將數據直接插入表的文本框。 或者通過Web服務將信息插入表中的任何其他方式。

namespace SkiFriendService
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class Service1 : System.Web.Services.WebService
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SkiFriendService.Properties.Settings.Setting"].ToString());


            [WebMethod]
            public string simpleMethod(String srt)
            {
                return "Hello "+srt;
            }


            [WebMethod]
            public int anotherSimpleMethod(int firstNum, int secondNum)
            {
                return firstNum + secondNum;
            }


            [WebMethod]
            public string[] getMessage(string from, string to) 
            {
                List<string> messages = new List<string>();
                con.Open();
                    string sqlquery = string.Format("select Message from MessageTable where Sender='{0}' and Receiver='{1}'", from, to);
                SqlCommand com = new SqlCommand(sqlquery, con);
                SqlDataReader sqlReader = com.ExecuteReader();
                while (sqlReader.Read()) 
                {
                    messages.Add(sqlReader.GetString(0));
                }
                con.Close();
                return messages.ToArray();
            }


            [WebMethod]
            public void InsertInfo(string sender, string receiver, string message)
            {
                List<string> messages = new List<string>();
                con.Open();
                string sql = "INSERT INTO MessageTable (Sender, Receiver, Message) VALUES (@Sender, @Receiver, @Message)";
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader sqlReader = com.ExecuteReader();
                while (sqlReader.Read())
                {
                    messages.Add(sqlReader.GetString(0));
                }
                con.Close();

            }





            }



            }

我希望我對此已經足夠清楚了。 我還在學習這個,所以很多這對我來說都是新的。 我將不勝感激任何幫助。 謝謝

        [WebMethod]
        public void InsertInfo(...)
        {
            ...
            SqlDataReader sqlReader = com.ExecuteReader();

如果要插入數據, 可能不希望在其名稱中使用DataReader 請改用DataAdapter


一個更大的問題

更大的問題是,您似乎已將代碼復制並粘貼到InsertInfo Web方法中而不進行讀取。 如果你已經閱讀了你的代碼,那么你的大腦應該已經鎖定了包括ReadReader在內的六個令牌中的至少一個。 鎖定之后,你的大腦應該對自己說:“自我,我在哪里更新或插入*任何東西?我似乎只是在東西。”

通過閱讀其他人的代碼,然后閱讀自己的代碼來培養這種技能。 根據我的經驗,只需閱讀自己的代碼就不會很快學會。 我們的大腦似乎讓我們的眼睛對自己的錯誤視而不見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM