C# step by step

begin programme
posts - 2, comments - 8, trackbacks - 0, articles - 0

2006年10月26日

昨天写一个代码感觉CurrencyManager运用在DataGrvdView中用处很大 所以摘录一些内容来学习参考


You use the CurrencyManager object if you want to keep data-bound controls synchronized with each other which means showing data from the same record. For example: if you want to add a TextBox control to a form and bind it to a column of a table (e.g., Customers.FirstName) in a DataSet (e.g., dSet), the control is going to communicate with the BindingContext object for this form. In turn, the BindingContext object is going to talk to the specific CurrencyManager object for the data the TextBox control is binding.

Every Windows Form has a BindingContext object keeping track of all the CurrencyManager objects on the Windows Form. CurrencyManager keeps track of the position in the data source. When you bind a data object to a control (i.e., TextBox), a CurrencyManager object is automatically assigned. If you bind several controls to the same data source, they share the same CurrencyManager.

In a normal case where you are using an ADO.NET database (connecting and closing database) and displaying the records, e.g., in a DataGrid, you never need the CurrencyManager object. But if you want to know the exact position within a data structure (e.g., table in your database) as I did, you have to use the CurrencyManager object because the CurrencyManager has the Position property for this purpose. You can, for example, manipulate the Position property in a Next or Previous or First or Last button which I did in my program as well.
For example:
If you want to know how many records are in a DataTable, you simply query the BindingContext object's Count property
this.BindingContext[dataset1,"PersonTable"].Count - 1 ;
If you want to get the current position from the BindingContext object:
this.BindingContext[dataset1, "PersonTable"].Position + 1;

After data binding, you call and initialize CurrencyManager for your table. Here is the method I used to initialize the CurrencyManager for the table "PersonTable":
public void fnSetCurrencyManager()
{
  currManager = (CurrencyManager)this.
  BindingContext [ datc.dSet.Tables["PersonTable"]] ;
}

posted @ 2006-10-26 20:14 开始编程 阅读(1381) | 评论 (2)编辑 收藏

做为学习过程中的札记 所写的都是本人自己的一些设计写法 只是想记录一下学习的点点嘀嘀。
转入正题:利用数据库提取用户验证信息实现登陆。
priviate void button1_Click(object sender,System.EventArgs e)
{
       SqlConnection conn=new SqlConnection("@Data Source="(local);Integrated Security=SSPI;Initial Catalog=DBApp"");
       SqlCommand cmd=conn.CreateCommand();
       cmd.CommandType=CommandType.Text;
     cmd.CommandType = CommandType.Text;
            if (txtUserName.Text.Length == 0)
            {
                MessageBox.Show("用户不能为空,请查询后输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select count(*) from UserList where UserName='" + txtUserName.Text + "'";
            conn.Open();
            if ((int)cmd.ExecuteScalar() == 0)
            {
                MessageBox.Show("不存在此用户,请检查后重新输入");
                txtPassWord.Focus();
                return;
            }
            cmd.CommandText = "select UserName, PassWord from UserList where PassWord='" +txtPassWord .Text + "'";
            SqlDataReader myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                if (txtPassWord.Text == myReader[0].ToString() && txtUserName.Text.Length != 0) ;
                MessageBox.Show("登录成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmMain fm = new frmMain();
                fm.ShowDialog();
            }

            else
            {
                MessageBox.Show("用户" + txtUserName.Text + "的密码不正确", "密码", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                txtPassWord.Focus();
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
}

posted @ 2006-10-26 19:57 开始编程 阅读(5422) | 评论 (6)编辑 收藏