这个登录c#代码有什么问题

时间:2013-02-22 19:46:15

标签: c# sql login ado.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;

namespace aukcijska_p
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                MessageBox.Show("Please insert correct values.", "Incorrect Username or password.");
            else
            {
                SqlConnection con = new SqlConnection(@"data source=(local);database=aukcija;integrated security=true;");
                con.Open();

                SqlCommand cmd = new SqlCommand("Select count(*) from users where name='" + username + "' and password='" + password + "'", con);

                Int32 returnedCount = (Int32)cmd.ExecuteScalar();

                if (returnedCount > 0)
                    new Window();
                else
                    MessageBox.Show("Wrong username or password");
            }

        }
        private void textBox1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {

        }
    }

和此:

<Window x:Class="aukcijska_p.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="227" Width="292">
    <Grid Height="196" Width="281">
        <Label Content="Username:" Height="28" HorizontalAlignment="Left" Margin="37,63,0,0" Name="label1" VerticalAlignment="Top" Width="75" />
        <Label Content="Password:" Height="28" HorizontalAlignment="Left" Margin="37,99,0,0" Name="label2" VerticalAlignment="Top" Width="75" />
        <Button Content="Confirm" Height="23" HorizontalAlignment="Left" Margin="157,133,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="112,68,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="112,101,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" TextChanged="textBox2_TextChanged" Visibility="Visible" />
    </Grid>
</Window>

它正常调试,但是当我按下确认时,我收到消息:程序停止工作......我在哪里犯了错误?有人可以帮帮我吗? 我用这个表创建了sql数据库:用户:user_ID,username,userpassword,user_level

继承人的形象: http://www39.zippyshare.com/v/97161315/file.html

1 个答案:

答案 0 :(得分:4)

Select count(*) from users where name=

应该是:

Select count(*) from users where username=

在错误消息中说明了这一点。 &#34;列名无效&#34;。 Cmon男人你需要更加努力!不要放弃!

但正如其他人所说,代码还存在许多其他问题。我建议阅读一般的数据库访问或学习绕过很多这些问题的实体框架。