第十五章 StringBuilder AppendLine

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

namespace ConsoleApplication1
{
    class Program
    {
        public void GetStudentList()
        {
            string strConn = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                conn.Open();
                Console.WriteLine("数据库打开");
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("select");
                sb.AppendLine("                studentno");
                sb.AppendLine("                ,studentname");
                sb.AppendLine("from");
                sb.AppendLine("           student");

                Console.WriteLine(sb.ToString());
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
                int i = (int)comm.ExecuteScalar();
            }
            catch (Exception)
            {

                Console.WriteLine("数据库操作失败!");
            }
            finally
            {
                conn.Close();
                Console.WriteLine("数据库关闭");
            }

        }
        static void Main(string[] args)
        {
            Program pro = new Program();
            pro.GetStudentList();
            Console.ReadLine();
        }
    }
}

你可能感兴趣的:(第十五章 StringBuilder AppendLine)