我目前正在尝试使用C#语言的Visual Studios WPF应用程序创建一个登录系统。每次Login_Button_Click
函数运行时,我都会收到一条错误消息。错误消息显示特定的代码行var DataReader = TeacherLoginsCommand.ExecuteReader();
我检查了无数的视频,很多视频似乎都在做同样的事情而没有任何问题。任何帮助表示赞赏。
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 MySql.Data.MySqlClient;
using System.Data;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow(){
InitializeComponent();
}
//When Login Button Is Clicked Do:
private void Login_Button_Click(Object sender, RoutedEventArgs e){
//Gets Username And Password From Loing Screen
string TeacherID = Teacher_ID.Text;
string TeacherPassword = Teacher_Password.Password;
//Connects To Database And Gets All Teacher Logins
string connectionString = "SERVER=localhost;DATABASE=teacherlogins;UID=root;PASSWORD=Password";
MySqlConnection TeacherLoginsConnection = new MySqlConnection(connectionString);
string CurrentQuery = "SELECT * from teacherloginstable where ID = " + Teacher_ID;
MySqlCommand TeacherLoginsCommand = new MySqlCommand(CurrentQuery, TeacherLoginsConnection);
TeacherLoginsConnection.Open();
var DataReader = TeacherLoginsCommand.ExecuteReader();
DataTable TeacherLoginsTable = new DataTable();
TeacherLoginsTable.Load(DataReader);
TeacherLoginsConnection.Close();
if (TeacherLoginsTable.Rows.Contains(TeacherPassword))
{
MessageBox.Show("Login Sucsessful", "Welcome" + Teacher_ID);
}
else {
MessageBox.Show("Details Not Recognised", "Error: Details Not Recognised");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}
XAML代码:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Alperton Login" Height="450" Width="625">
<Border Padding="50">
<StackPanel Margin="5">
<Image Source="download.png" Height="90" Margin="0, 0, 0, 20"/>
<TextBlock Text="Teacher ID" FontWeight="Bold" FontSize="20" Padding="0,10"/>
<TextBox Padding="5" x:Name="Teacher_ID"/>
<TextBlock Text="Teacher Password" FontWeight="Bold" FontSize="20" Padding="0,10"/>
<PasswordBox Padding="5" x:Name="Teacher_Password" />
<Button Content="Login" Margin="0,10" Padding="0, 3" FontWeight="Bold" FontSize="15" Click="Login_Button_Click"/>
</StackPanel>
</Border>
错误讯息:
MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.TextBox' at line 1'
答案 0 :(得分:0)
而不是:
string TeacherID = Teacher_ID.Text;
写
int TeacherID = Convert.toInt32(Teacher_ID.Text);
答案 1 :(得分:0)
Teacher_ID
是XAML代码中的TextBox id(x:Name),它引用TextBox。您必须在sql语句中发送其text属性。
Sql语句必须是这样的
SELECT * FROM teacherloginstable where ID = " + Teacher_ID.Text
;
答案 2 :(得分:0)
你是自己不幸决定给出两个不同目的非常相似的变量的受害者。
您有一个名为Teacher_ID
的文本框,并将其文本放在TeacherID
中。你有可能在查询中使用了错误的吗?
您是否尝试使用调试器或Debug.WriteLine
或Console.WriteLine
检查要发送给MySQL的SQL查询字符串?我希望你能看到如下内容:
SELECT * from teacherloginstable where ID = System.Windows.Controls.TextBox
(System.Windows.Controls.TextBox
是您在ToString()
上致电TextBox
时获得的。)
你看到了问题吗?
您可以使用正确的变量名
来修复它 string CurrentQuery = "SELECT * from teacherloginstable where ID = " + TeacherID;
但实际上,请不要这样做。请不要通过连接值来构建SQL字符串。使用参数化SQL要好得多:
string CurrentQuery = "SELECT * from teacherloginstable where ID = @teacherID";
MySqlCommand TeacherLoginsCommand = new MySqlCommand(CurrentQuery, TeacherLoginsConnection);
TeacherLoginsCommand.Parameters.AddWithValue("@teacherID", TeacherID);