我为 Android 开发了一个应用程序,它应该使用 WCF 服务并使用 JSON 广告记录插入 MS SQL Server 数据库。我不知道为什么它没有在数据库中插入新行,你能帮我找出错误在哪里吗?服务1.svc.sc:
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 Ads_ID=@Ads_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 Ads_Content=@Ads_Content,Ads_Category=@Ads_Category,Ads_Phone=@Ads_Phone where Ads_ID=@Ads_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 Ads_ID=@Ads_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:
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;
}
}