Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将json数据从webservice放置到sql数据库_C#_Jquery_Ajax_Sql Server 2005_Json - Fatal编程技术网

C# 将json数据从webservice放置到sql数据库

C# 将json数据从webservice放置到sql数据库,c#,jquery,ajax,sql-server-2005,json,C#,Jquery,Ajax,Sql Server 2005,Json,这是我检索用户名的特定webservice代码,如何编写c代码将json数据放置到webservice中的sql数据库中,并在html页面中显示确认信息 public class AjaxService : System.Web.Services.WebService { [WebMethod] public bool CheckUserNameAvailability(string userName) { List<String> userN

这是我检索用户名的特定webservice代码,如何编写c代码将json数据放置到webservice中的sql数据库中,并在html页面中显示确认信息

public class AjaxService : System.Web.Services.WebService
{
    [WebMethod]
    public bool CheckUserNameAvailability(string userName)
    {
        List<String> userNames = new List<string>() { "azamsharp", "johndoe", "marykate", "alexlowe", "scottgu" };

        var user = (from u in userNames
                    where u.ToLower().Equals(userName.ToLower())
                    select u).SingleOrDefault<String>();

        return String.IsNullOrEmpty(user) ? true : false; 

    }

}

}

按照以下方式装饰您的AjaxService类:

[WebService(Description = "Web services to query the book database.", Namespace ="http://www.your-site-url.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class AjaxService: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool CheckUsernameAvailability(string userName, Options options)
    {
        return this.WriteToSql(options);
    }

    private bool WriteToSql(Options options)
    {
        bool result = false;

        try
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

            SqlCommand command = new SqlCommand("INSERT_OPTION", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@Option1", SqlDbType.NVarChar, 20).Value = options.Option1;
            command.Parameters.Add("@Optoin2", SqlDbType.Bit).Value = (options.Option2 == true ) ? 1 : 0;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            result = true;
        }
        catch(Exception ex)
        {
            //do some logging here...
            result = false;
        }

        return result;
    }
}

[Serializable]
public class Options
{
    public string Option1 { get; set; }
    public bool Option2 { get; set; }
}
那么您的JQuery将如下所示:

$( function(){

    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: "AjaxService.asmx/CheckUsernameAvailability",
        data: { userName: 'TestUsername', options: { Option1: 'test', Option2: 'false' } },
        success: function(msg) {
            //display acknowledgement here. DO NOT USE EVAL! It's bad.

            var result = eval(msg);
            alert(result);
        } 
    });

});

按如下方式装饰您的AjaxService类:

[WebService(Description = "Web services to query the book database.", Namespace ="http://www.your-site-url.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class AjaxService: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool CheckUsernameAvailability(string userName, Options options)
    {
        return this.WriteToSql(options);
    }

    private bool WriteToSql(Options options)
    {
        bool result = false;

        try
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

            SqlCommand command = new SqlCommand("INSERT_OPTION", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@Option1", SqlDbType.NVarChar, 20).Value = options.Option1;
            command.Parameters.Add("@Optoin2", SqlDbType.Bit).Value = (options.Option2 == true ) ? 1 : 0;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            result = true;
        }
        catch(Exception ex)
        {
            //do some logging here...
            result = false;
        }

        return result;
    }
}

[Serializable]
public class Options
{
    public string Option1 { get; set; }
    public bool Option2 { get; set; }
}
那么您的JQuery将如下所示:

$( function(){

    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: "AjaxService.asmx/CheckUsernameAvailability",
        data: { userName: 'TestUsername', options: { Option1: 'test', Option2: 'false' } },
        success: function(msg) {
            //display acknowledgement here. DO NOT USE EVAL! It's bad.

            var result = eval(msg);
            alert(result);
        } 
    });

});