i got a problem.
I want to write a Programm to Insert some stuff into a Mysql Database. In the first time i make for every column in the Database a own String and used this to make the SQL Query. But in the most tables i got more then 100 columns. And i dont want to make 100 Variables. So i decided to make it with a datatable and Array. But now i dont know how to fill the names of the columns into the array. Here is the part of the code that do that.
private void button_npccreate_click(object sender, EventArgs e)
{
//Umwandlung von C# Variable in Mysql Variable
MySqlCommand command = new MySqlCommand();
command.Parameters.AddWithValue("@server", server);
command.Parameters.AddWithValue("@port", port);
command.Parameters.AddWithValue("@user", user);
command.Parameters.AddWithValue("@password", mysqlpassword);
command.Parameters.AddWithValue("@world", db_world);
//Connection zu Mysql Server
string myConnectionString = "Server=" + server + ";Port=" + port + ";Database=" + db_world + ";Uid=" + user + ";Pwd=" + mysqlpassword + ";";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand cmd;
cmd = connection.CreateCommand();
string readquery = "SELECT * FROM quest_template;";
MySqlDataAdapter read_adapter = new MySqlDataAdapter(readquery, connection);
DataTable tableRead = new DataTable();
connection.Open();
read_adapter.Fill(tableRead);
connection.Close();
int columns = 0;
if(tableRead.Rows.Count > 0)
{
foreach(DataColumn dc in tableRead.Columns)
{
columns++;
}
}
else
{
MessageBox.Show("The table is empty. Please check your Database.");
}
string [] columns_array = new string[columns];
foreach (DataColumn dc in tableRead.Columns)
{
// Here i must fill the Array i think. But i dont know how to do this.
}
}