Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually it is reterive data on console application. it is work correctly but i want to update data


C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project
{
    class data
    {
        static void getdata()
        {

            SqlConnection con = new SqlConnection("Data Source=SANKALPA-PC;Initial Catalog=libray;Integrated Security=True");
            con.Open();
            DataSet ds = new DataSet();
            try
            {
                string sql = "select * from book1";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    printrow(Convert.ToString(dr["bookid"]), Convert.ToString(dr["title"]), Convert.ToString(dr["author"]), Convert.ToString(dr["publisher"]), Convert.ToString(dr["price"]));

                }
             
                con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
        }
    

        static void printline()
        {
            Console.WriteLine(new string('-', 80));
        }
        static void printrow(string bookid, string title, string author, string publisher, string price)
        {
            Console.WriteLine(string.Format("|{0}|{1}|{2}|{3}|{4}|",AlignCenter(bookid,17),AlignCenter(title,17),AlignCenter(author,17),AlignCenter(publisher,17),AlignCenter(price,17)));
        }
        static string AlignCenter(string text, int width)
        {

            if (string.IsNullOrEmpty(text))
            {
                return new string(' ', width);

            }

            else
            {

                //return text.PadRight(width - (width – text.Length) / 2).PadLeft(width);
                return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);

            }

        }
        
          
   

        static void Main(string [] args)
        {
            printline();
            printrow("bookid","title","author","publisher","price");
            printline();
            getdata();
            Console.ReadKey();
        }

        
    }
}



actually it is reterive data on console application. it is work correctly but i want to update data ,please help me.
Posted
Comments
BillWoodruff 1-Jan-14 2:03am    
Please define exactly how an "update" of the data is triggered, and exactly what "update" means.

Do you mean read the whole DataSet again, and clear the Console, and then write the current values to the Console ?
rahul katoch 2021 15-Feb-22 11:11am    
write a C# program using list add,update,delete but without using data base in console Application

1 solution

Console is updated by simply rewriting all its content, or only the parts which need replacement. Note that its possible to output in console using absolute positioning of characters:
http://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.console.setcursorposition(v=vs.110).aspx[^].

Happy New Year!

—SA
 
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