ado.net 数据库连接的两方式种

DataAdapter:
 string connectionString = "data source=127.0.0.1;Database=dong;user id=sa;password=521314";
            SqlConnection cn = new SqlConnection(connectionString);
            //SqlConnection conn = new SqlConnection("connectionString");

            string sql2 = "SELECT * FROM yingjia_login WHERE(USERS_NAME='李明')";

            SqlDataAdapter reader = new SqlDataAdapter(sql2, cn);
            DataSet ds = new DataSet();
            reader.Fill(ds, "login");
            reader.Dispose();
            dataGridView1.DataSource = ds.Tables["login"];

DataReader:

            string connectionString = "data source=127.0.0.1;Database=dong;user id=sa;password=521314";
            SqlConnection cn = new SqlConnection(connectionString);
            //SqlConnection conn = new SqlConnection("connectionString");
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;

            string sql2 = "SELECT USERS_NAME,USERS_PASSWORD, USERS_ID FROM yingjia_users WHERE(USERS_NAME='" + textBox1.Text + "')";
            cmd.CommandText = sql2;
            cmd.CommandType = CommandType.Text;
    while (datar.Read())
                    {
                        //判断用户输入是否正确
                        if (datar["USERS_PASSWORD"].ToString().Trim() != textBox2.Text.Trim())
                        {
                            MessageBox.Show("用户密码不正确,请重新输入!");
                            textBox2.Focus();
                          
                            return;
                       
                        }

posted @ 2013-07-29 08:35  早晚会飞的懒猪  阅读(173)  评论(0编辑  收藏  举报