我有问题。
我想编写一个程序来将一些东西插入 Mysql 数据库。我第一次为数据库中的每一列创建了一个自己的字符串,并用它来制作 SQL 查询。但在大多数表中,我得到了 100 多列。而且我不想制作 100 个变量。所以我决定用数据表和数组来做。但现在我不知道如何将列名填充到数组中。这是执行此操作的代码部分。
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.
}
}