我想在c#中創建一個可重用的MySQL數據庫類,但我擔心它保持安全(可能的業務用途)。 我已經做了大量的閱讀,並且人們推薦使用參數將數據添加到表中並獲取它等。我的問題是通過多個不同的表保持類可重用 我的插入方法當前(在數據庫類中):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();
}
我怎樣才能讓這個更安全?
嗨,哈比。你能否提供一個代碼示例,我知道我需要使用參數,但是我所做的所有嘗試都要花費我對類的可重用需求,因爲需要將精確字段硬編碼到它中。 –
@MarkTallentire,您仍然可以使用您擁有的類,您應該修改它以便在方法中打開連接。請參閱:http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples -mysqlcommand.html#connector-net-examples-mysqlcommand-parameters參數 – Habib