-5
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

4

1 回答 1

4
Select count(*) from users where name=

应该:

Select count(*) from users where username=

它在错误消息中这么说。“列名无效”。Cmon 人,你需要更加努力!不要放弃!

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

于 2013-02-22T20:06:05.427 回答