ASP.NET

19.jQuery - 메모장 웹서비스 설계

Godffs 2009. 11. 19. 15:00
반응형
웹 서비스를 사용해서 MS-SQL 연결 사용하기 위함입니다.

MemoServices.cs

using System;

using System.Web.Services;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Web.Configuration;

using System.Data;

using System.Web;

using System.Web.Script.Services;

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class MemoService : System.Web.Services.WebService

{

    [WebMethod]

    // JSON 형태로 반환하려면 ResponseFormat 지정해야

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public List<MemoEntity> GetMemos()

    {

        List<MemoEntity> lst = new List<MemoEntity>();

 

        SqlConnection con = new SqlConnection(

           WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


        SqlCommand cmd = new SqlCommand(

           "Select * From Memos Order By Num Desc", con);


        SqlDataAdapter da = new SqlDataAdapter(cmd);


        DataSet ds = new DataSet();

        da.Fill(ds, "Memos");

 

        DataTable dt = ds.Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++)

        {

            MemoEntity objMemo = new MemoEntity();

            objMemo.Num = Convert.ToInt32(dt.Rows[i]["Num"]);

            objMemo.Name = dt.Rows[i]["Name"].ToString();

            objMemo.Email = dt.Rows[i]["Email"].ToString();

            objMemo.Title = dt.Rows[i]["Title"].ToString();

            objMemo.PostDate = Convert.ToDateTime(dt.Rows[i]["PostDate"]);

            objMemo.PostIP = dt.Rows[i]["PostIP"].ToString();

            lst.Add(objMemo);

        }

 

        return lst;

    }

}



반응형