如何在C#控制台应用程序上更新数据 [英] How to update data on C# console application

查看:184
本文介绍了如何在C#控制台应用程序上更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上它是控制台应用程序上的重新数据。它工作正常但我想更新数据





actually it is reterive data on console application. it is work correctly but i want to update data


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();
        }

        
    }
}







实际上它是控制台应用程序的reterive数据。它工作正常,但我想更新数据,请帮助我。




actually it is reterive data on console application. it is work correctly but i want to update data ,please help me.

推荐答案

只需重写所有内容或仅需要更换的部分即可更新控制台。请注意,可以使用字符的绝对定位在控制台中输出:

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 [ ^ ]。



新年快乐!



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


这篇关于如何在C#控制台应用程序上更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆