I'm studying some codes of Php and C# and would like to know how can I convert this code in C#?
$data = {
//field name in the database, amount to be inserted
'name' => 'myName',
'email' => 'myEmail',
'number' => 9999999999
};
function DBCreate($table, array $data, $ReturnId = false){
//table prefix
$table = DB_PREFIX . '_' . $table;
//clean sql injection
$data = DBEscape($data);
//creates an array based on database fields
$fields = implode(',', array_keys($data));
$values = "'".implode("', '", $data)."'";
//query sql
$query = "INSERT INTO {$table} ( {$fields} ) VALUES ( {$values})";
//call function that executes the query
return DBExecute($query, $ReturnId);
}
This code inserts data into any database table passing only the table name along with an array this array consists of pairs where the key is the field name in the database and the value is the value to be inserted into the database, last parameter is if necessary to recover the value of the ID entered in the database.
or which elements can study in order to convert?
yet got it:
void DBCreate(string table, Dictionary<string,string> Data) {
var fields = string.Join(", ",Data.Keys.ToArray());
var values = string.Join(", ",Data.Values.ToArray());
string sql = "INSERT INTO " + table + " ( " + fields + " ) VALUES ( " + values + " )";
print(sql);
}
using:
var myDictionary = new Dictionary<string, string>() {
{"Name", "MeuName"},
{"Passowrd", "mypass"}
};
DBCreate("tabela", myDictionary);
this is Usuario class:
public string Name = "";
public string Passowrd = "";
public DateTime Date = Convert.ToDateTime("01/01/1991");
public string Email = "";
public string Username = "";
public int Cash = 0;
public int GP = 0;
public string IdCell = "";
how I can get all the names of the variables of the Usuario class, or how could I convert it into a list?
I decided:
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
Usuario user = new Usuario();
user.setName("Something");
user.setEmail(123456789);
var newUser = new Dictionary<string, string>();
foreach (FieldInfo field in typeof(Usuario).GetFields(bindingFlags))
{
print(field.Name + " Values => " + field.GetValue(user).ToString());
newUser.Add(field.Name, field.GetValue(user).ToString());
}
thank you all!