Create WebService for Getting Details of Tables from SQL Database in ASP.NET

Introduction

As mentioned in the blog header the code is for creating WebService of retreiving Table details through Sql Database.

        [System.Web.Script.Services.ScriptMethod()]

        [System.Web.Services.WebMethod]

        public static List<string> getSqlinfo(string prefixText)

        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString());

            conn.Open();

 

            // If the table name starts from UserDetails we can write User and then search for the details as the query contains a like i.e % character which will search for all the tables named with User%.

 

            SqlCommand cmd = new SqlCommand("SELECT NAME FROM SYS.TABLES WHERE NAME LIKE @Name+'%'", conn);

            cmd.Parameters.AddWithValue("@Name", prefixText.ToUpper());

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            List<string> tablename = new List<string>();

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

            {

                tablename.Add(dt.Rows[i][0].ToString());

            }

            return tablename;

        }

Next Recommended Reading Pass JSON Array to WebServices