C#VS2010访问SQL数据库

private void button1_Click(object sender, EventArgs e)
{
string ConnectionString = "Server=(local);Integrated Security=SSPI;Database=student";
SqlConnection myconn = new SqlConnection(ConnectionString);
myconn.Open();
MessageBox.Show("连接成功!");
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr[字段].ToString());
}
}
访问SQL Server2008 student数据库中的学生表,请改一下代码

Sql和字段
要替换成你实际的变量。
给你代码不是照抄就行的,得看懂。

你少个变量Sql ,Sql = "select * from 学生表"

SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(Sql, conn); //这里的sql是变量,可以定义sql,如select 语句,修改这里即可
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr[字段].ToString());
}

两个地方注意一下:
1、少个 string sql = "select * from 学生表";
2、Console.WriteLine(dr["姓名"].ToString());

SqlCommand cmd = new SqlCommand(Sql, conn);
例如
SqlCommand cmd = new SqlCommand("select 学号 from 学生表 where 学号 = @学号", myconn);//学号为变量
mycmd.Parameters.Add("@学号", SqlDbType.NVarChar).Value = TextBox1.Text.Trim();//变量的值=asp中TextBox1控件中的值

SqlConnection myconn = new SqlConnection(ConnectionString);
myconn.Open();
MessageBox.Show("连接成功!");
SqlConnection conn = new SqlConnection(ConnectionString);
干嘛写两次

private void button1_Click(object sender, EventArgs e)
{
string Sql;
string ConnectionString = "Server=(local);Integrated Security=SSPI;Database=student";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
MessageBox.Show("连接成功!");
Sql = "select* from 学生";
SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr["姓名"].ToString());
}
}
修改之后,没有报错,但只能弹出连接数据库成功之后的对话框,怎么没访问的数据