我的想法是通过 C# (3.5) Winforms 应用程序通过 MySQL .NET 连接器 6.2.2 与 MySQL 数据库对话,为插入/更新/选择创建一些通用类。
例如:
public void Insert(string strSQL)
{
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
然后从程序中的任何地方,我都可以通过传递一个 SQL 查询字符串来运行一个有/没有用户输入的查询。
阅读 SO 开始让我知道这可能会导致 SQL 注入攻击(对于任何用户输入值)。是否有清理输入的 strSQL 或者我需要在每个需要执行数据库功能的方法中创建单独的参数化查询?
更新1:
我的最终解决方案如下所示:
public void Insert(string strSQL,string[,] parameterValue)
{
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
for(int i =0;i< (parameterValue.Length / 2);i++)
{
cmd.Parameters.AddWithValue(parameterValue[i,0],parameterValue[i,1]);
}
cmd.ExecuteNonQuery();
this.CloseConnection();
}}