{
SqlConnection lo_conn = new SqlConnection(@"Data Source=SOFTENGER\SOFENGER;Initial Catalog=school;Integrated Security=True");//创建数据库连接 连接字符串
lo_conn.Open();//与数据进行连接
SqlCommand lo_cmd = new SqlCommand();//创建命令对象 主要想SQL数据库发送指令
lo_cmd.CommandText = "select * from person";//执行的指令
lo_cmd.Connection = lo_conn;//指定连接对象,也就是上面创建的数据库
lo_cmd.ExecuteNonQuery(); //这一条仅仅执行指令,没有任何返回集
SqlDataReader lo_reader = lo_cmd.ExecuteReader();//执行指令,并有返回集
while(lo_reader.Read())//读取数据内容
{
int lie = lo_reader.FieldCount;//获得列数
for (int i = 0; i < lie; i++)//遍历每列
textBox1.Text += lo_reader[i].ToString() + "\t";
textBox1.Text += "\r\n";
}
lo_conn.Close();//关闭连接
}
连接数据库时,可以使用vs中“工具->连接到数据库" 来获取连接字符串
入门级