using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace SqlConn { class Program { static void Main(string[] args) { string strConnectionString = @"server=localhost;database=StudentInfo;uid=sa;pwd=tianshi520"; SqlConnection sqlConn = new SqlConnection(strConnectionString); string strSql = "select * from student"; SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn); if (sqlConn.State == ConnectionState.Open) { sqlConn.Close(); } sqlConn.Open(); SqlDataReader sqlDataReader = sqlCmd.ExecuteReader(); do { while (sqlDataReader.Read()) Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", sqlDataReader["Sno"], sqlDataReader["Sname"], sqlDataReader["Ssex"], sqlDataReader["Sage"], sqlDataReader.GetString(4)); } while (sqlDataReader.NextResult()); sqlDataReader.Close(); sqlConn.Close(); } } }
时间: 2025-01-23 10:16:58