簡體   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