using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string connStr1 = "server=LAPTOP-M89EEOQH\\SQLEXPRESS;database =course;uid=nihonge;pwd=nihonge";
string queryString ="SELECT id, name FROM dbo.t1;";
CreateCommand(queryString, connStr1);
}
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
SqlDataReader thisSqlDataReader = command.ExecuteReader();
while (thisSqlDataReader.Read())
{
Console.WriteLine("{0} {1} ", thisSqlDataReader[0], thisSqlDataReader[1]);
}
//关闭读取
thisSqlDataReader.Close();
//关闭连接
command.Connection.Close();
}
}
}
}