简体   繁体   中英

How convert this function PHP in c#?

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 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!

Well it seems the code is building up a table name and an insert query with multiple value pairs. I don't do C# but it should be quit easy with some research on "insert sql c#". eg here is first result I found.

Do you want to keep the ordered map that PHP uses as an array? Since PHP uses an ordered associative array you might be able to use something like an ordered dictionary that is provided in .NET, or a list of key value pairs . The rest of it is escaping the values and building strings. You will have to explicitly define the return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM