大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > 登陆界面Login代码

登陆界面Login代码

关键词:登陆界面Login  阅读(748) 赞(13)

[摘要]

本文介绍了一个登陆界面的代码,供大家参考下。

最终界面:

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>

后台代码:

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();
        }

数据库:access 数据库



相关评论