MySQL connector .NET : Parameters

  • 5258
  • 0
  • 2016-02-24

摘要:MySQL connector .NET : Parameters

最近要在.NET環境下連MySQL資料庫
MySQL提供了MySQL Connector .NET來在.NET環境下連接MySQL
不過在使用Parameter的時候有點更改, .NET下要使用 @var 的方式

(以前也是?var, 不過後來改成@var)
不過MySQL Connector .NET必須要設定成 ?var
所以原本的寫法

SqlCommand command = new SqlCommand("SELECT * from user WHERE user_name = @id AND user_passwd = @passwd", conn);
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add("@id", SqlDbType.VarChar, 12).Value = id;
command.Parameters.Add("@passwd", SqlDbType.VarChar, 160).Value = passwd;
command.Prepare();
Console.WriteLine(command.CommandText);
MySqlDataReader reader = command.ExecuteReader();

 要改成

// 要改成?id, ?passwd
MySqlCommand command = new MySqlCommand("SELECT * from user WHERE user_name = ?id AND user_passwd = ?passwd", conn);
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add("?id", MySqlDbType.VarChar, 12).Value = id;
command.Parameters.Add("?passwd", MySqlDbType.VarChar, 160).Value = passwd;
command.Prepare();
Console.WriteLine(command.CommandText);
MySqlDataReader reader = command.ExecuteReader();