Unable to get CommandText
Posted by: Moon Chen
Date: September 27, 2018 07:13AM

Hi guys,

I’m a newcomer to MySQL. I'm using C# to connect a MySQL database. I can connect to it but I can't get information from it. I've tried few methods but it didn't work.

Here is connecting part.
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "xxxx";

Method 1:
MySqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT count(*) FROM myTable";
label3.Text = myCommand.ExecuteScalar().ToString();

Method 2:
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "SELECT count(*) FROM myTable";
label3.Text = myCommand.ExecuteScalar().ToString();

Method 3:
MySqlCommand myCommand = new MySqlCommand("SELECT count(*) FROM myTable", myConnection);
label3.Text = myCommand.ExecuteScalar().ToString();

Actually I can't tell difference from all of those methods, and all methods cannot execute. They throw a same exception when code execute myCommand.ExecuteScalar(). I set a breakpoint and found CommandText is null even when I set it as "SELECT count(*) FROM myTable".

Here is my exception:

在 MySql.Data.MySqlClient.MySqlStream.ReadPacket()
在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
在 MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
在 MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
在 MySql.Data.MySqlClient.MySqlDataReader.NextResult()
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar()
在 Daily_Report.Form1.button_Generate_Click(Object sender, EventArgs e) 在 C:\Users\Administrator\Desktop\Daily Report\Daily Report\Form1.cs 中: 第 28 行
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 Daily_Report.Program.Main() 在 C:\Users\Administrator\Desktop\Daily Report\Daily Report\Program.cs 中: 第 19 行

I don't know why this happens, please help me find this. Thanks.

Any help would be greatly received,


Options: ReplyQuote

Written By
Unable to get CommandText
September 27, 2018 07:13AM

Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.