我正在尝试在 c# 中创建一个可重用的 MySQL 数据库类,但我担心保持它的安全(可能的业务用途)。我做了很多阅读,人们建议使用参数将数据添加到表中并获取它等。我的问题是通过许多不同的表保持类可重用我的插入方法目前(在数据库类中):
public void Insert(string incomingQuery) //Insert Statement
{
//Assign the incomingQuery for use.
string query = incomingQuery;
//Open Database Connection
if(this.OpenConnection() == true)
{
//Create command and assign the query and connection from the constructor
MySqlCommand command = new MySqlCommand(query, connection);
//Execute the command
command.ExecuteNonQuery();
//Close the connection
this.CloseConnection();
}
}
我的Create
方法从另一个类(Users
)传递 SQL 查询:
public void Create()
{
//Add a new user to the database
string sqlQuery = "INSERT INTO Users(first_name, last_name, pc_username) VALUES('" + firstName + "','" + lastName + "','" + userName + "');";
database.Insert(sqlQuery);
Login();
}
我怎样才能使它更安全?