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