繁体   English   中英

C#用户注册表格问题

[英]C# User Registration Form Issue

我正在尝试创建一个简单的用户注册表单,但是在第29行, myReader行为异常,一个应用程序在那里冻结。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
            string Query = "insert into truex2_kuba.users (uid,password) values('" + uid.Text + "','" + pwd.Text + "');";
            MySqlConnection myConn = new MySqlConnection(myConnection);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("Użytkownik Stworzony");
            while (myReader.Read())
            {

            }
            myConn.Close();
        }
    }
}

错误:

MySql.Data.MySqlClient.MySqlException was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Unknown column 'password' in 'field list'
  Number=1054
  Source=MySql.Data
  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 WindowsFormsApplication1.Form5.button1_Click(Object sender, EventArgs e) in C:\Users\terleckk\Downloads\WindowsFormsApplication1 - Copy (1)\WindowsFormsApplication1 - Copy\WindowsFormsApplication1\Form5.cs:line 29
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication1.Program.Main() in C:\Users\terleckk\Downloads\WindowsFormsApplication1 - Copy (1)\WindowsFormsApplication1 - Copy\WindowsFormsApplication1\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       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: 

由于要进行插入查询,因此必须使用executeNonQuery而不是ExecuteReader 还使用参数:

try
{
     string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
     string Query = "insert into truex2_kuba.users (uid,password) values(@user,@pass);";
     using (MySqlConnection myConn = new MySqlConnection(myConnection))
      {
        MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn); 
        cmdDataBase.Parameters.AddWithValue("@user",uid.Text);
        cmdDataBase.Parameters.AddWithValue("@pass",pwd.Text);
        myConn.Open();
        cmdDataBase.ExecuteNonQuery();                
        MessageBox.Show("Użytkownik Stworzony");             
      }
}
catch (SQlException ex)
 {
   MessageBox.Show(ex.Message);
 }

冻结可能是由于数据库往返或某些网络问题引起的。 或者,您可以尝试在单独的线程上运行以下代码吗? 这样,您的应用将永远不会冻结,因为以下功能在单独的线程上而不是在主UI线程上运行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
 public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        Thread dbThread;

        private void button1_Click(object sender, EventArgs e)
        {
            dbThread = new Thread(DbRead);
            dbThread.Start ();
        }


        void DbRead()
        {
            string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
            string Query = "insert into truex2_kuba.users (uid,password) values('" + uid.Text + "','" + pwd.Text + "');";
            MySqlConnection myConn = new MySqlConnection(myConnection);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("Użytkownik Stworzony");
            while (myReader.Read())
            {

            }
            myConn.Close();

        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM