c# - 通过 C# 将 WPF 表单中的数据插入 MySQL 数据库

标签 c# mysql wpf ado.net mysql-connector

我在通过 C# 将值插入 MySQL 数据库时遇到了一个特殊问题。

我有四个控件,我正在从 WPF 表单中收集数据;三个是文本框,一个是日期选择器。

连接到数据库后,填写表格。我一直在使用以下值:

txtIdNumber: 1
dtpDateOfBirth = 7/30/2013 (today)
txtMenarcheAge = 14
txtSport1 = Soccer

这是正在清洗的代码:

if (MessageBox.Show("Are you sure you want to submit these changes to the database? Just making sure...", "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                //database logic HERE.
                DateTime birthday = (DateTime)dtpDateOfBirth.SelectedDate;
                string birthdayString = birthday.ToString("yyyy-MM-dd");

                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;

                Console.WriteLine(Convert.ToString(dtpDateOfBirth.SelectedDate));

                //check each box that MUST contain an entry, then submit it to the database if it's correct
                if (txtIdNumber.Text != "" && dtpDateOfBirth.SelectedDate != null)
                {                        
                    cmd.CommandText = "INSERT INTO TestSubject VALUES(NULL, @subjectId, @subjectDOB)";
                    cmd.Prepare();
                    cmd.Parameters.AddWithValue("@subjectId", txtIdNumber.Text);
                    cmd.Parameters.AddWithValue("@subjectDOB", "'" + birthdayString + "'");
                    Console.WriteLine(birthdayString);
                    Console.WriteLine(cmd.Parameters["@subjectId"].Value);
                    Console.WriteLine(cmd.Parameters["@subjectDOB"].Value);
                    cmd.ExecuteNonQuery();
                }
                else if (txtIdNumber.Text != "" && dtpDateOfBirth.SelectedDate != null && txtMenarcheAge.Text != "")
                {
                    cmd.CommandText = "INSERT INTO TestSubject VALUES(@subjectId, @subjectDOB, @subjectMenarche)";
                    cmd.Prepare();
                    cmd.Parameters.AddWithValue("@subjectId", txtIdNumber.Text);
                    cmd.Parameters.AddWithValue("@subjectDOB", birthdayString);
                    cmd.Parameters.AddWithValue("@subjectMenarche", txtMenarcheAge.Text);
                    cmd.ExecuteNonQuery();
                }
            }

首先,我将日期选择器中的日期时间转换为类似于 MySQL 使用的日期时间。然后,我将其转换为字符串。

接下来,我设置必需的 MySqlCommand 对象,并将其 Connection 属性设置为 conn,该属性是在另一个类中建立的。

之后进行检查以确保表单有效。必须提供测试对象的 ID 号和生日,因此首先进行检查。这就是我们遇到麻烦的地方。我对 MySQL 中的准备语句比较陌生,所以我使用 this示例,以便让自己定向。在街区

cmd.CommandText = "INSERT INTO TestSubject VALUES(NULL, @subjectId, @subjectDOB)";
cmd.Prepare();
cmd.Parameters.AddWithValue("@subjectId", txtIdNumber.Text);
cmd.Parameters.AddWithValue("@subjectDOB", "'" + birthdayString + "'");
Console.WriteLine(birthdayString);
Console.WriteLine(cmd.Parameters["@subjectId"].Value);
cmd.ExecuteNonQuery();

我在运行时遇到以下异常:

MySql.Data.MySqlClient.MySqlException was unhandled


HResult=-2147467259
  Message=Column 'subjectId' cannot be null
  Source=MySql.Data
  ErrorCode=-2147467259
  Number=1048
  StackTrace:
       at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
       at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
       at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
       at mu_kinect_project_data_form.MainWindow.btnSubmitWholeForm_Click(Object sender, RoutedEventArgs e) in c:\Users\asm3kf.TIGERS\Documents\mu_kinect_project_data_form\mu_kinect_project_data_form\MainWindow.xaml.cs:line 315
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run()
       at mu_kinect_project_data_form.App.Main() in c:\Users\asm3kf.TIGERS\Documents\mu_kinect_project_data_form\mu_kinect_project_data_form\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

我不确定它从哪里得到subjectIdnull的想法。它正在正常接收输入。

编辑:添加 MySQL 脚本,因为这是拼图的一半。

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `kinect7` DEFAULT CHARACTER SET utf8 ;
USE `kinect7` ;

-- -----------------------------------------------------
-- Table `kinect7`.`TestSubject`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kinect7`.`TestSubject` (
  `subjectId` INT(11) NOT NULL ,
  `subjectDOB` DATE NOT NULL ,
  `subjectMenarche` DATE NULL DEFAULT NULL ,
  PRIMARY KEY (`subjectId`) ,
  UNIQUE INDEX `subjectId_UNIQUE` (`subjectId` ASC) )
ENGINE = InnoDB
COMMENT = '     ';


-- -----------------------------------------------------
-- Table `kinect7`.`SubjectInjury`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kinect7`.`SubjectInjury` (
  `injuryDate` DATE NOT NULL ,
  `injuryKnee` VARCHAR(5) NOT NULL ,
  `TestSubject_subjectId` INT(11) NOT NULL ,  
  `surgeryPerformed` VARCHAR(10) NULL,  
  PRIMARY KEY (`TestSubject_subjectId`, `injuryKnee`, `injuryDate`) ,
  CONSTRAINT `fk_PreviousInjury_TestSubject1`
    FOREIGN KEY (`TestSubject_subjectId` )
    REFERENCES `kinect7`.`TestSubject` (`subjectId` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


-- -----------------------------------------------------
-- Table `kinect7`.`SubjectJumps`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kinect7`.`SubjectJumps` (
  `jumpDate` DATETIME NOT NULL ,
  `rightAnkleRatio` DECIMAL(6,3) NULL DEFAULT NULL ,
  `leftAnkleRatio` DECIMAL(6,3) NULL DEFAULT NULL ,
  `rightKneeValgus` DECIMAL(6,3) NULL DEFAULT NULL ,
  `leftKneeValgus` DECIMAL(6,3) NULL DEFAULT NULL ,
  `rightKneeFlexion` DECIMAL(6,3) NULL DEFAULT NULL ,
  `leftKneeFlexion` DECIMAL(6,3) NULL DEFAULT NULL ,
  `testSubject_subjectId` INT(11) NOT NULL ,
  PRIMARY KEY (`jumpDate`, `testSubject_subjectId`) ,
  INDEX `fk_subjectJumps_testSubject_idx` (`testSubject_subjectId` ASC) ,
  CONSTRAINT `fk_subjectJumps_testSubject`
    FOREIGN KEY (`testSubject_subjectId` )
    REFERENCES `kinect7`.`TestSubject` (`subjectId` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


-- -----------------------------------------------------
-- Table `kinect7`.`SubjectSports`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kinect7`.`SubjectSports` (
  `sport` VARCHAR(10) NOT NULL ,
  `TestSubject_subjectId` INT(11) NOT NULL ,
  PRIMARY KEY (`TestSubject_subjectId`, `sport`) ,
  CONSTRAINT `fk_SubjectSports_TestSubject1`
    FOREIGN KEY (`TestSubject_subjectId` )
    REFERENCES `kinect7`.`TestSubject` (`subjectId` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;   

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

最佳答案

要弄清楚错误到底发生在哪里有点困难,但您可能需要在 SQL 查询中定义列名称:

cmd.CommandText = "INSERT INTO TestSubject (Subject,DOB) VALUES (@subjectId, @subjectDOB)";

这样,您就不必为缺失的列显式传入 null,并且您可以确定每个值分配给哪些列。

关于c# - 通过 C# 将 WPF 表单中的数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17955753/

相关文章:

c# - 在 C# 中连接来自两个数据库的表

c# - 是否有人围绕 StringBuilders 或 Streams 实现了 Regex 和/或 Xml 解析器?

c# - 从俄罗斯网站读取 XML 时的编码问题

c# - 创建自定义控件的最佳 WPF 面板

wpf - 手动调整窗口大小、最大化或还原时如何调整 TabControl 的大小

c# - 测试从另一个方法调用的方法

mysql - 应该使用哪个查询?从 MySQL Explain 推导

javascript - 为什么没有数据消息显示? (试图创建谷歌图表)

mysql - 5分钟吧使用mysql

c# - MVVM light RelayCommand 触发两次