MySQL Parameters.AddWithValue 不工作 C#
MySQL Parameters.AddWithValue not working C#
我一直在尝试编写连接代码来连接数据库。一切进展顺利,但我 运行 遇到了一个我似乎无法解决的 st运行ge 问题。
当我尝试向我的查询添加参数时,它没有检索到数据。但是当我关闭参数时,它确实检索到正确的数据。
数据访问层如下所示。 (我知道这很不安全,但我才刚刚开始):
public static class UserDA
{
private static MySqlCommand cmd = null;
private static DataTable dt;
private static MySqlDataAdapter da;
public static User RetrieveUser(String username)
{
// Create the query
const String query = "SELECT `Username`, `Password` FROM login WHERE `Username` = '@Username';";
cmd = DbHelper.Select(
query,
new Dictionary<String, String>
{
{"@Username", username}
});
//Setup model
User user = null;
if (cmd == null) return null;
// Get the data and add it to the user model
dt = new DataTable();
da = new MySqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dataRow in dt.Rows)
{
user = new Models.User(
dataRow["Username"].ToString(),
dataRow["Password"].ToString());
}
return user;
}
}
Select 方法如下所示:
public static class DbHelper
{
private static MySqlConnection _connection;
private static MySqlCommand _cmd;
private static DataTable _dataTable;
private static MySqlDataAdapter _adapter;
public static void EstablishConnection()
{
// Gets a connection to the database. Code works.
}
/// <summary>
/// Give the query and a dict with the param name and value
/// </summary>
public static MySqlCommand Select(String query, Dictionary<String , String> param)
{
try
{
if (_connection != null)
{
_connection.Open();
_cmd = _connection.CreateCommand();
_cmd.CommandType = CommandType.Text;
_cmd.CommandText = query;
foreach (KeyValuePair<String, String> keyValuePair in param)
{
_cmd.Parameters.AddWithValue(keyValuePair.Key, keyValuePair.Value);
}
_cmd.ExecuteScalar();
_connection.Close();
}
}
catch (Exception e)
{
_connection?.Close();
}
return _cmd;
}
}
最后是用户模型:
public class User
{
public String Username;
public String Password;
public User(String username = "", String password = "")
{
Username = username;
Password = password;
}
}
当我尝试从中获取数据时出现问题。如果我使用带有参数的 select 查询,它不起作用,但是只要我将参数更改为实际值,它就会恢复正常。在这一点上我真的很挠头。我是不是忘记了什么地方的步骤?
谢谢!
尝试删除 SQL 代码中@Username 两边的引号。
之前
SELECT `Username`, `Password` FROM login WHERE `Username` = '@Username';
之后
SELECT `Username`, `Password` FROM login WHERE `Username` = @Username;
如果这不起作用,请尝试将字典从 更改为 。
从您的代码中删除它
_cmd.ExecuteScalar();
_connection.Close();
为了更好的编码风格,我强烈建议将 class 用户更改为
public class User
{
public string Username {get; set;}
public string Password {get; set;}
public User()
{
}
public User(string username, string password)
{
Username = username;
Password = password;
}
}
```
我一直在尝试编写连接代码来连接数据库。一切进展顺利,但我 运行 遇到了一个我似乎无法解决的 st运行ge 问题。
当我尝试向我的查询添加参数时,它没有检索到数据。但是当我关闭参数时,它确实检索到正确的数据。
数据访问层如下所示。 (我知道这很不安全,但我才刚刚开始):
public static class UserDA
{
private static MySqlCommand cmd = null;
private static DataTable dt;
private static MySqlDataAdapter da;
public static User RetrieveUser(String username)
{
// Create the query
const String query = "SELECT `Username`, `Password` FROM login WHERE `Username` = '@Username';";
cmd = DbHelper.Select(
query,
new Dictionary<String, String>
{
{"@Username", username}
});
//Setup model
User user = null;
if (cmd == null) return null;
// Get the data and add it to the user model
dt = new DataTable();
da = new MySqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dataRow in dt.Rows)
{
user = new Models.User(
dataRow["Username"].ToString(),
dataRow["Password"].ToString());
}
return user;
}
}
Select 方法如下所示:
public static class DbHelper
{
private static MySqlConnection _connection;
private static MySqlCommand _cmd;
private static DataTable _dataTable;
private static MySqlDataAdapter _adapter;
public static void EstablishConnection()
{
// Gets a connection to the database. Code works.
}
/// <summary>
/// Give the query and a dict with the param name and value
/// </summary>
public static MySqlCommand Select(String query, Dictionary<String , String> param)
{
try
{
if (_connection != null)
{
_connection.Open();
_cmd = _connection.CreateCommand();
_cmd.CommandType = CommandType.Text;
_cmd.CommandText = query;
foreach (KeyValuePair<String, String> keyValuePair in param)
{
_cmd.Parameters.AddWithValue(keyValuePair.Key, keyValuePair.Value);
}
_cmd.ExecuteScalar();
_connection.Close();
}
}
catch (Exception e)
{
_connection?.Close();
}
return _cmd;
}
}
最后是用户模型:
public class User
{
public String Username;
public String Password;
public User(String username = "", String password = "")
{
Username = username;
Password = password;
}
}
当我尝试从中获取数据时出现问题。如果我使用带有参数的 select 查询,它不起作用,但是只要我将参数更改为实际值,它就会恢复正常。在这一点上我真的很挠头。我是不是忘记了什么地方的步骤?
谢谢!
尝试删除 SQL 代码中@Username 两边的引号。
之前
SELECT `Username`, `Password` FROM login WHERE `Username` = '@Username';
之后
SELECT `Username`, `Password` FROM login WHERE `Username` = @Username;
如果这不起作用,请尝试将字典从
从您的代码中删除它
_cmd.ExecuteScalar();
_connection.Close();
为了更好的编码风格,我强烈建议将 class 用户更改为
public class User
{
public string Username {get; set;}
public string Password {get; set;}
public User()
{
}
public User(string username, string password)
{
Username = username;
Password = password;
}
}
```