简体   繁体   English

尝试在DataGrid上显示表时出现MySQL错误

[英]MySQL Error While Trying To Display A Table On a DataGrid

This is the code I'm using: 这是我正在使用的代码:

private void btn_search_Click(object sender, EventArgs e)
{
    try
    {
        MySqlConnection con = new MySqlConnection(@"Data Source=localhost;port=3306;Initial Catalog=dp;User Id=root;password=''");
        con.Open();
        DataTable dt = new DataTable();
        MySqlDataAdapter SDA = new MySqlDataAdapter("SELECT * FROM dp WHERE id LIKE " + txt_id.Text, con);
        SDA.Fill(dt);
        dataGridView1.DataSource = dt;
    }
    catch (MySqlException ex)
    {
        Console.WriteLine("StackTrace:" + ex.StackTrace);
    }

Problem is, It throws a MySQL exception, 问题是,它抛出一个MySQL异常,

Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll 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.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 引发异常:MySql.Data.dll StackTrace中的'MySql.Data.MySqlClient.MySqlException':MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&受影响的行,Int64&insertId)的MySql.Data.MySqlClient.MySqlStream.ReadPacket() MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&受影响的行,Int64&insertId)位于MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean force)位于MySql.Data.MySqlClient.MySqlDataReader.NextResult() System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior行为)处的MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior行为) .Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)位于System.Data.Common.DbDataAdapter.Fill(DataTable [] dataTables,Int32 startRecord,Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) at Dispatching_Software.SearchUsers.btn_search_Click(Object sender, EventArgs e) in C:\\Users\\Owner\\documents\\visual studio 2015\\Projects\\Dispatching Software\\Dispatching Software\\SearchUsers.cs:line 34 maxRecords,IDbCommand命令,CommandBehavior行为)位于C:\\ Users \\ Owner \\ documents \\ visual studio 2015 \\ Projects中Dispatching_Software.SearchUsers.btn_search_Click(Object sender,EventArgs e)处的System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) \\ Dispatching Software \\ Dispatching Software \\ SearchUsers.cs:第34行

What I'm trying to do is search for users within a table and display them, The problem seems to be SDA.Fill(dt) ; 我想要做的是在表中搜索用户并显示他们,问题似乎出在SDA.Fill(dt)

You don't need to use con.Open(); 您不需要使用con.Open(); when you are using MySqlDataAdapter . 当您使用MySqlDataAdapter Also you should always use parameterized queries to avoid SQL Injection : 另外,您应该始终使用参数化查询来避免SQL注入

MySqlDataAdapter SDA = new MySqlDataAdapter("SELECT * FROM dp WHERE id LIKE '%' + @a + '%'", con);
SDA.SelectCommand.Parameters.AddWithValue("@a", txt_id.Text);

Although specify the type directly and use the Value property is more better than AddWithValue : 尽管直接指定类型并使用Value属性比AddWithValue更好:

 MySqlDataAdapter SDA = new MySqlDataAdapter("SELECT * FROM dp WHERE id LIKE @a", con);
SDA.SelectCommand.Parameters.Add("@a", MySqlDbType.VarChar, 200).Value = "%" + txt_id.Text;

Can we stop using AddWithValue() already? 我们可以停止使用AddWithValue()吗?

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

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