C#_控制台打印数据库里的数据信息

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
SqlConnection sqlconnection = new SqlConnection("SERVER=.;Initial Catalog=MySchool;User ID=sa;Password=123456");//数据库连接
            string sql = string.Format("SELECT * FROM Student");
            sqlconnection.Open();
            SqlCommand sqlcommand = new SqlCommand(sql, sqlconnection);
            SqlDataAdapter da = new SqlDataAdapter(sqlcommand);
            DataSet ds = new DataSet();
            da.Fill(ds);


            for (int i = 0; i < ds.ToString().Length; i++)
            {
                Console.WriteLine(ds.Tables[0].Rows[i]["LoginId"]);//输入要打印的表中字段名
                Console.WriteLine(ds.Tables[0].Rows[i]["LoginPwd"]);//输入要打印的表中字段名


            }
            Console.ReadLine();
            


        }
    }
}

猜你喜欢

转载自blog.csdn.net/Lan_Se_Tian_Ma/article/details/80951226