ConsoleApplication1
{
    class Program
    {
        
static void Main(string[] args)
        {
            SqlConnection conn 
= new SqlConnection("server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");
            
string sql = @"select companyname,contactname from customers where contactname like 'M%'";
            
try
            {
                conn.Open();
                SqlCommand cmd 
= new SqlCommand(sql,conn);
                SqlDataReader reader 
= cmd.ExecuteReader();
               
                Console.WriteLine(
"\t{0}{1}","CompanyName".PadRight(25),"ContactName".PadRight(20));
                Console.WriteLine(
"\t{0}{1}","===========".PadRight(25),"===========".PadRight(20));
                
while (reader.Read())
                {
                    Console.WriteLine(
"\t{0}|{1}",reader[0].ToString().PadRight(25),reader[1].ToString().PadRight(20));
                }
            }
            
catch (SqlException ex)
            {
                Console.WriteLine(
"Error:{0}", ex.Message);
            }
            
finally
            {

            } Console.ReadLine();
        }
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-07-27
  • 2021-12-04
  • 2021-12-27
  • 2022-12-23
猜你喜欢
  • 2021-10-21
  • 2021-07-18
  • 2022-01-19
  • 2021-09-26
  • 2021-11-17
  • 2021-07-05
相关资源
相似解决方案