0

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!

5
  • 3
    this is not a translation service Commented Sep 27, 2015 at 19:40
  • You might do well by learning about strings in C# and ADO.NET fundamentals. Commented Sep 27, 2015 at 19:44
  • Could you at least explain what this does and someone may give you a way to do it without knowing PHP well. Commented Sep 27, 2015 at 20:06
  • What is usuario class here? Commented Sep 28, 2015 at 5:26
  • @EmpereurAiman i add the Usuario class. Commented Sep 30, 2015 at 6:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, so I need to convert the object into a Dicionary , this will help me a lot ! thnk
For future reference, quoting from links / some code examples are usually good in an answer
1

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#". e.g. here is first result I found.

1 Comment

I have an object that each attribute is the name of the respective table field in the database , I'm looking for ways to convert this object into an array , but without success I have a data entry function using Parameters.Add more I did not want to write all query's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.