visual stidio api create method and passing the value

 

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Connection String Path

//============================java Code Start =========================================

// authentication with password

string conStr = @"Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=Book;Data Source=DESKTOP-FD8825Q\SQLEXPRESS";

// authentication with password try secend way

        //string conStr = @"Data Source=DESKTOP-FD8825Q\SQLEXPRESS;Initial Catalog=Book;Integrated Security=True";

// authentication without password

String dbpath = @"Data Source=DESKTOP-FD8825Q\SQLEXPRESS;Initial Catalog=Book;Integrated Security=True";
=========================================End==========================================

 

//--------------------------------------------Code Detail and Working---------------------------------------------------------

Connection String Path

//============================java Code Start =========================================

string conStr = @"Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=Book;Data Source=DESKTOP-FD8825Q\SQLEXPRESS";

        //string conStr = @"Data Source=DESKTOP-FD8825Q\SQLEXPRESS;Initial Catalog=Book;Integrated Security=True";
=========================================End==========================================

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Get Data in Database from API

//============================java Code Start =========================================

public HttpResponseMessage GetAllBooks()

        {

            SqlConnection con = new SqlConnection(conStr);

            con.Open();

            string query = "select bid, title from [bookEntery]";

            SqlCommand command = new SqlCommand(query, con);

          

            SqlDataAdapter sda = new SqlDataAdapter(command);

            DataSet ds = new DataSet();

            sda.Fill(ds);

            con.Close();

            return Request.CreateResponse(HttpStatusCode.OK, ds.Tables[0]);

        }
=========================================End==========================================

 

 

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Save in Database from API function

//============================java Code Start =========================================

[HttpPost]

        public HttpResponseMessage SaveBook(int id, string title,int edition, int price)

        {

            SqlConnection con = new SqlConnection(conStr);

            con.Open();

            string query = string.Format(

                "insert into [bookEntery] values ('{0}','{1}','{2}','{3}')",

                id,

                title,

                edition,

                price);

            SqlCommand command = new SqlCommand(query, con);

            command.ExecuteNonQuery();

            con.Close();

            return Request.CreateResponse(HttpStatusCode.OK, "Data saved");

        }

=========================================End==========================================

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Update in Database from API function

//============================java Code Start =========================================

[HttpPost]

        public HttpResponseMessage UpdateBook(int id, string title, int edition, int price)

        {

            SqlConnection con = new SqlConnection(conStr);

            con.Open();

            string query = string.Format(

                "update  [bookEntery] set [title] = '{1}', [edition]='{2}'," +

                "[prine]='{3}' where id ='{0}'",

                id,

                title,

                edition,

                price);

            SqlCommand command = new SqlCommand(query, con);

            command.ExecuteNonQuery();

            con.Close();

            return Request.CreateResponse(HttpStatusCode.OK, "Data Updated");

        }

=========================================End==========================================

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Class Create

//============================java Code Start =========================================

public class User

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

    }

=========================================End==========================================

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Show data in Database from API function by using Class

//============================java Code Start =========================================

[HttpGet]

        public HttpResponseMessage Display()

        {

            SqlConnection con = new SqlConnection(dbpath);

            con.Open();

            String query = "select * from bookEntery";

            SqlCommand cmd = new SqlCommand(query, con);

            SqlDataReader sdr = cmd.ExecuteReader();

            List<bookEntery> book = new List<bookEntery>();

            while (sdr.Read())

            {

                bookEntery b= new bookEntery();

                b.bid = int.Parse(sdr[0].ToString());

                b.title = sdr[1].ToString();

                b.edition = sdr[2].ToString();

                b.Price = int.Parse(sdr[3].ToString());

 

                book.Add(b);

            }

 

            con.Close();

            return Request.CreateResponse(

                HttpStatusCode.OK,

               book

                );

 

        }

=========================================End==========================================

 

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Search by ID in Database from API function by using Class

//============================java Code Start =========================================

[HttpGet]

        public HttpResponseMessage SearchID(int id)

        {

            SqlConnection con = new SqlConnection(dbpath);

            con.Open();

            String query = "select * from bookEnter where eid= '"+id+"'";

            SqlCommand cmd = new SqlCommand(query, con);

            SqlDataReader sdr = cmd.ExecuteReader();

            List<bookEntery> book = new List<bookEntery>();

            while (sdr.Read())

            {

                bookEntery b = new bookEntery();

                b.bid = int.Parse(sdr[0].ToString());

                b.title = sdr[1].ToString();

                b.edition = sdr[2].ToString();

                b.Price = int.Parse(sdr[3].ToString());

 

                book.Add(b);

            }

 

            con.Close();

            return Request.CreateResponse(HttpStatusCode.OK, book);

 

        }

=========================================End==========================================

//--------------------------------------------Code Detail and Working-------------------------------------------------------

Body throw in Database from API function by using Class

//============================java Code Start =========================================

public class BookReg

        {

           public int id;

           public string title;

           public int edition;

           public int price;

            public int qty;

        }

        [HttpPost]

        public HttpResponseMessage getbook([FromBody]BookReg p)

        {

           

            SqlConnection con = new SqlConnection(conStr);

            con.Open();

            string query = string.Format(

                "insert into bookReg values ('{0}','{1}','{2}','{3}')",

                p.id,

                p.title,

                p.price,

                p.qty

                );

            SqlCommand command = new SqlCommand(query, con);

            command.ExecuteNonQuery();

            con.Close();

            return Request.CreateResponse(HttpStatusCode.OK, "hello");

        }

=========================================End==========================================

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.