2013-10-03 1 views
1

WCF 서비스를 사용하고 JSON Advertisement 레코드를 사용하여 MS SQL Server 데이터베이스에 삽입해야하는 Android 용 앱을 개발했습니다. 왜 데이터베이스에 새로운 행을 삽입하지 않는지 모르겠다. 오류가 어디 있는지 알아낼 수 있습니까? Service1.svc.sc :MS SQL Server에 삽입하기위한 WCF 서비스

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

namespace wcf_ads_Proj 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. 
    public class Service1 : IService1 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AdsCon"].ConnectionString); 

     public string InsertAdsRegDetails(RegDetails adsregdet) 
     { 
      string Status; 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 

      SqlCommand cmd = new SqlCommand("insert into Tbl_Ads(Ads_Content,Ads_Category,Ads_Phone) VALUES (@Ads_Content, @Ads_Category, @Ads_Phone)", con); 
      cmd.Parameters.AddWithValue("@Ads_Content", adsregdet.Ads_Content); 
      cmd.Parameters.AddWithValue("@Ads_Category", adsregdet.Ads_Category); 
      cmd.Parameters.AddWithValue("@Ads_Phone", adsregdet.Ads_Phone); 

      int result = cmd.ExecuteNonQuery(); 
      if (result == 1) 
      { 
       Status = adsregdet.Ads_Content + " registered successfully"; 
      } 
      else 
      { 
       Status = adsregdet.Ads_Content + " could not be registered"; 
      } 
      con.Close(); 
      return Status; 
     } 

     public DataSet GetAdsRegDetails() 
     { 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand cmd = new SqlCommand("Select * from Tbl_Ads", con); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      return ds; 
     } 

     public DataSet FetchUpdatedRecords(RegDetails regdet) 
     { 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand cmd = new SqlCommand("select * from Tbl_Ads where [email protected]_ID", con); 

      cmd.Parameters.AddWithValue("@Ads_ID", regdet.Ads_ID); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      return ds; 
     } 

     public string UpdateAdsRegDetails(RegDetails regdet) 
     { 
      string Status; 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand cmd = new SqlCommand("update Tbl_Ads set [email protected]_Content,[email protected]_Category,[email protected]_Phone where [email protected]_ID", con); 
      cmd.Parameters.AddWithValue("@Ads_ID", regdet.Ads_ID); 
      cmd.Parameters.AddWithValue("@Ads_Content", regdet.Ads_Content); 
      cmd.Parameters.AddWithValue("@Ads_Category", regdet.Ads_Category); 
      cmd.Parameters.AddWithValue("@Ads_Phone", regdet.Ads_Phone); 
      //cmd.Parameters.AddWithValue("@Password", regdet.Password); 
      //cmd.Parameters.AddWithValue("@ContactNo", regdet.ContactNo); 
      int result = cmd.ExecuteNonQuery(); 
      if (result == 1) 
      { 
       Status = "Record updated successfully"; 
      } 
      else 
      { 
       Status = "Record could not be updated"; 
      } 
      con.Close(); 
      return Status; 
     } 
     public bool DeleteAdsRegDetails(RegDetails regdet) 
     { 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 
      SqlCommand cmd = new SqlCommand("delete from Tbl_Ads where [email protected]_ID", con); 

      cmd.Parameters.AddWithValue("@UserRegId", regdet.Ads_ID); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      return true; 
     } 

     public string InsertAds(RegDetails regdet) 
     { 
      string Status; 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 

      SqlCommand cmd = new SqlCommand("insert into Tbl_Ads(Ads_Content,Ads_Category,Ads_Phone) VALUES (@Ads_Content, @Ads_Category, @Ads_Phone)", con); 
      cmd.Parameters.AddWithValue("@Ads_Content", regdet.Ads_Content); 
      cmd.Parameters.AddWithValue("@Ads_Category", regdet.Ads_Category); 
      cmd.Parameters.AddWithValue("@Ads_Phone", regdet.Ads_Phone); 
      //cmd.Parameters.AddWithValue("@Password", regdet.Password); 
      //cmd.Parameters.AddWithValue("@ContactNo", regdet.ContactNo); 
      int result = cmd.ExecuteNonQuery(); 
      if (result == 1) 
      { 
       Status = regdet.Ads_Content + " registered successfully"; 
      } 
      else 
      { 
       Status = regdet.Ads_Content + " could not be registered"; 
      } 
      con.Close(); 
      return Status; 
     } 

    } 
} 

IService1.cs :

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

namespace wcf_ads_Proj 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract] 
     [WebInvoke(Method = "Get", 
      UriTemplate = "RegAds", 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json)] 
     string InsertAds(RegDetails regdet);  

     [OperationContract] 
     string InsertAdsRegDetails(RegDetails regdet); 

     [OperationContract] 
     DataSet GetAdsRegDetails(); 

     [OperationContract] 
     DataSet FetchUpdatedRecords(RegDetails regdet); 

     [OperationContract] 
     string UpdateAdsRegDetails(RegDetails regdet); 

     [OperationContract] 
     bool DeleteAdsRegDetails(RegDetails regdet); 
    } 


    // Use a data contract as illustrated in the sample below to add composite types to service operations. 
    [DataContract] 
    public class RegDetails 
    { 
     int p_Ads_ID; 
     string p_Ads_Content = string.Empty; 
     String p_Ads_Date = string.Empty; 
     string p_Ads_Phone = string.Empty; 
     string p_Ads_Category = string.Empty; 

     [DataMember] 
     public int Ads_ID 
     { 
      get { return p_Ads_ID; } 
      set { p_Ads_ID = value; } 
     } 
     [DataMember] 
     public string Ads_Content 
     { 
      get { return p_Ads_Content; } 
      set { p_Ads_Content = value; } 
     } 
     [DataMember] 
     public string Ads_Date 
     { 
      get { return p_Ads_Date; } 
      set { p_Ads_Date = value; } 
     } 
     [DataMember] 
     public string Ads_Phone 
     { 
      get { return p_Ads_Phone; } 
      set { p_Ads_Phone = value; } 
     } 
     [DataMember] 
     public string Ads_Category 
     { 
      get { return p_Ads_Category; } 
      set { p_Ads_Category = value; } 
     } 

    } 
} 

AddMemberActivity.java :을 yor에서

package com.example.twittersearch; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONStringer; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class AddMemberActivity extends Activity { 


    private class SendPostData extends AsyncTask<String, Void, String> 
    { 
     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
       EditText TxtContent=(EditText) findViewById(R.id.txtUsername); 
       String content=TxtContent.getText().toString(); 
       EditText TxtCategory=(EditText) findViewById(R.id.TxtPassword); 
       String category=TxtCategory.getText().toString(); 
       EditText txtPhone=(EditText) findViewById(R.id.TxtCountry); 
       String country=txtPhone.getText().toString(); 

      HttpPost request = new HttpPost("http://www.sayedyousif.com/ads_wcf/Service1.svc/RegAds"); 
      request.setHeader("Accept", "application/json");    
      request.setHeader("Content-type", "application/json"); 
      JSONStringer GetUserInfo; 
      try { 
       GetUserInfo = new JSONStringer() 

         .object() 
         .key("regdet") 
          .object() 
           .key("Ads_Content").value(content)         
           .key("Ads_Category").value(category) 
           .key("Ads_Phone").value(country) 
          .endObject() 
         .endObject(); 

      StringEntity entity = new StringEntity(GetUserInfo.toString()); 
       Log.d("Test", GetUserInfo.toString()); 
      request.setEntity(entity); 

      // Send request to WCF service 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpResponse response = httpClient.execute(request); 
      Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); 
      } 
      catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show(); 
     } 
    } 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_member); 

     Button btnadd=(Button) findViewById(R.id.btnAdd); 
     btnadd.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       SendPostData sendpostdata=new SendPostData(); 
       sendpostdata.execute(); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.add_member, menu); 
     return true; 
    } 

} 
+0

를 호출? – Piyush

+0

어떤 오류/문제가 있는지 정확하게 알려주십시오 – SalientBrain

답변

0

당신이

을 사용하는 WCF 편물 ... (방법은 = "GET"// (A GET)

를 호출 호출하고 데이터가 WCF 서비스에 안드로이드에서 도달 했 AddMemberActivity.java는 POST

+0

WebInvoke (Method = "Post")로 변경 하시길 권장합니다. –

+0

@SayedYousif 예 – christiangobo

+1

Piyush, 내 android json이 WCF 서비스로 이전하지 않았지만 다른 것을 구현 한 경우 이 http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crudoperations that worked! –

관련 문제