登陆界面Login

最终界面:

 
XMAL 代码:
  <Grid >
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Image Source="Image\AiMeiLi.jpg"  Margin="5"/>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="Auto"/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions >
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock Text="用户名" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2"/>
                <TextBox  Grid.Column="1"  VerticalAlignment="Center" Name="tbxLoginName"  Margin="2"/>
                <TextBlock Text="密码"  Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="2"/>
                <PasswordBox   Grid.Column="1" Grid.Row="1"  VerticalAlignment="Center"  Name="tbxLoginPasswords"  Margin="2"  />
                <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right"  >
                    <Button Content="登陆" Name="btnLogin" Margin="2" Click="btnLogin_Click" />
                    <Button  Content="取消"  Name="btnCancle" Margin="2" Click="btnCancle_Click" />
                </StackPanel>
            </Grid>
        </Grid>
View Code

 

后台代码:

private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AiMeiLi.accdb"))
           {
                string sqlText = @"select count(*) from [User] where UserName='" + tbxLoginName.Text + "' and UserPasswords='" + tbxLoginPasswords.Password + "'";
                using (OleDbCommand cmd=new OleDbCommand (sqlText, conn))
                {
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    int n=System.Convert.ToInt32( cmd.ExecuteScalar());
                    if (n > 0)
                    {
                        MainWindow mainW = new MainWindow();
                        mainW.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错");
                    }
                }
           }
        }

        private void btnCancle_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
View Code

 

数据库:access 数据库

 

 

posted @ 2013-10-09 17:44  碧风Befeng  阅读(1367)  评论(6编辑  收藏  举报