2016-05-27 6 views
0

私は単純なユーザ登録フォームを作成しようとしていますが、myReaderが不正であり、アプリがフリーズしています。C#ユーザ登録フォームの問題

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(); 
     } 
    } 
} 

ERROR:

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: 
+0

エラーメッセージが表示されますか? –

+0

ああ、その 'MisbehavingException'。真剣に、あなたのコードの周りにtry/catchをラップし、例外メッセージを読んでください。 – Crowcoder

+1

あなたの質問とは別に、[パラメータ化されたクエリ](https://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)を使い、使い捨てオブジェクトを '使用する 'で囲みます。 – DonBoitnott

答えて

1

凍結があるため、データベースの往復または一部のネットワークの問題の発生する可能性があります。別のスレッドで次のコードを実行しようとすることもできますか?このようにして、以下の関数はメインの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(); 

     } 
    } 
} 
+0

これを試しましたが、まだエラーが発生しました。 'MySql.Data MySql.Data.dllで.MySqlClient.MySqlException 'が発生しました。 – Truex

+0

オリジナルの質問の下に例外の詳細を投稿して編集できますか? –

+0

はい、元の投稿に追加されました – Truex

1

あなたがexecuteNonQuery代わりのExecuteReaderを使用する必要がINSERTクエリを作っているからです。また、パラメータを使用します。

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); 
} 
+0

提供されたコードを変更しましたが、cmdDataBaseで停止してエラーが発生しました。 'MySql.Data.MySqlClient.MySqlException'の未処理の例外 – Truex