JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » .Net Framework » .net 家族语言: C#, VB.net F#等  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 請問一下關於WebService取得SQLServer資料的相關問題
罪惡感





發文: 13
積分: 0
於 2013-09-29 18:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好,目前我在進行有關於Web Service取得SQL Server資料的程式撰寫,設想Web Service可以取得SQL Server內Table中_id最大的資料項,也就是要取得最新一筆的資料

程式碼如下:
WebService1.asmx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using StockManageWebservice;
 
namespace WebService
{
    /// <summary>
    /// WebService1 的摘要描述
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        DBOperations dbOperation = new DBOperations();
 
        [WebMethod(Description = "取得目前Temp")]
        public int getTemp()
        {
            return dbOperation.getTemp();
        }
    }
}
 


DBOperations.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
 
namespace StockManageWebservice
{
  
 
    public class DBOperations : IDisposable
    {
        public static SqlConnection sqlCon;
        int Temp;
        private String ConServerStr = @"Data Source=BEVIS-PC;Initial Catalog=TempDateSQLServer;Integrated Security=True";
 
        //Creat方法
        public void DBOperation()
        {
            if (sqlCon == null)
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = ConServerStr;
                sqlCon.Open();
            }
        }
 
        //Close方法
        public void Dispose()
        {
            if (sqlCon != null)
            {
                sqlCon.Close();
                sqlCon = null;
            }
        }
 
        public int getTemp() 
        {
            try
            {
                string sql = "SELECT TOP 1 _id,Temp FROM TempDate ORDER BY _id DESC";
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                SqlDataReader reader = cmd.ExecuteReader();
                Temp = int.Parse(reader["Temp"].ToString());
                reader.Close();
                cmd.Dispose();
                return Temp;
            }
            catch (Exception)
            {
                return Temp = 1;
            }
        }
    }
}


目前是卡在我無法取得SQL Server的資料出來,以Web Service的呼叫函式測試時Temp的值都會跑出1,請各位大大幫我看一下我的語法有哪邊有出錯嗎?


reply to postreply to post
» JWorld@TW »  .Net Framework » .net 家族语言: C#, VB.net F#等

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8