日期:2014-05-17  浏览次数:20822 次

C#连接到数据库
小弟刚刚开始学用C#来实现对数据库的连接访问。老师给了一段代码

namespace AdoTest
{
    public partial class Form1 : Form
    {
        


        private SqlDataReader dr;
        private DataSet ds;
        private BindingSource bs;

        public Form1()
        {
            InitializeComponent();
            ds = new DataSet();
            bs = new BindingSource();
        }
        private void ChuShiHua()
        {
            string ssql= "select * from student";

            //SqlCommand cmd=new SqlCommand(ssql,cn);

            SqlDataAdapter ad = new SqlDataAdapter(ssql,ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
            
            ds.Clear();
            ad.Fill(ds, "student");
            
            //textBox2.Text = ds.Tables["student"].Rows[0]["sname"].ToString();
            bs.DataSource = ds;
            bs.DataMember ="student";

            bindingNavigator1.BindingSource = bs;
            dataGridView1.DataSource = bs;

            
            //dataGridView1.DataSource = ds.Tables["student"];
            //textBox1.DataBindings.Add("Text", ds.Tables["student"], "sno");
            //bindingSource1.DataSource = ds.Tables["student"];
            //bindingNavigator1.BindingSource=new BindingSource(ds,"student");
            //bindingNavigator1.BindingSource.DataMember="student";
        }

private SqlDataReader dr;
private DataSet ds;
private BindingSource bs;
不明白这三行的作用,为什么要放在开头以及用PRIVATE,求大神解答,谢谢!