Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to android development. Now i'm trying android demo application. I struck to access stored procedure by sending multiple data type data as parameter from ANDROID.

Created Rest web service to access data from DB and method mentioned below which accept 3 parameter and return string.

C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Newtonsoft.Json;

namespace JSONWebAPI
{
    public class ServiceAPI : IServiceAPI
    {
        private SqlConnection SqlCon;
        private SqlCommand SqlCmd;
        private SqlDataAdapter SqlAdapt;
        private DataSet ds;
        private SqlDataReader SqlReader;
        private StringBuilder Sb;

        private dynamic Result;

        public ServiceAPI()
        {
            SqlCon = DBConnect.getConnection();
        }

        public String Getdata(String strSqlQuery, SqlParameter[] arrSqlParam, Boolean IsStoredProcedure)
        {
            using (SqlCon)
            {
                if (SqlCon.State == ConnectionState.Closed || SqlCon.State == ConnectionState.Broken)
                    SqlCon.Open();
                SqlCmd = new SqlCommand(strSqlQuery, SqlCon);
                if (IsStoredProcedure)
                {
                    SqlCmd.CommandType = CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddRange(arrSqlParam);
                }
                SqlCmd.ExecuteNonQuery();
                SqlAdapt = new SqlDataAdapter(SqlCmd);
                ds = new DataSet();
                SqlAdapt.Fill(ds);

                Result = ConvertTableToJson(ds);

                SqlAdapt.Dispose();
                SqlCmd.Dispose();
                SqlCon.Dispose();
            }
            return Result;
        }

        private String ConvertTableToJson(DataSet dsDownloadJson)
        {
            return JsonConvert.SerializeObject(dsDownloadJson, Formatting.Indented);
        }
    }
}


I have searched google but still I didn't get solution. How to achieve this please guide me on this.
Posted

1 solution

Much like Console.WriteLine, the SqlParameter constructor is overloaded, and the first argument to all overloads is the name of the parameter, followed by additional arguments as needed to specify its type and value. So, you dimension your SqlParameter array, and fill it with a new SqlParameter object for each query parameter. Although all are of a type (SqlParameter ), each instance is more like a NameValueCollection item, and you can mix and match types as needed.

For additional info, please see https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900