我想创建编辑窗口来编辑数据库中的行。C#获取数据库行中的所有项目
如何从数据库行读取所有项目?
我的代码到现在为止是:
private void FirmCustomerValues()
{
config conf = new config();
connection = new MySqlConnection(conf.connection_string);
try
{
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM firmenkunden WHERE id='" + firmCustomerID + "'",connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
foreach (var row in reader)
{
System.Windows.Controls.TextBox textbox = new TextBox();
// Textbox Properties
textbox.Width = 159;
textbox.Name = "";
textbox.Text = reader[1].ToString();
textboxWP.Children.Add(textbox);
}
}
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
但是,这是行不通的。 有没有人有一个想法如何解决这个问题?
我想我也需要for循环。如果答案很简单,那么我对C#很感兴趣。
textBoxWP是如何设置的? –
不应该这个读者[1] .ToString(); be row.ToString(); ? –
你可能会考虑使用第三方库“Dapper”。编程更容易,对SQL注入更安全,速度也更快。 – NotTelling