gpt4 book ai didi

c# - 使用 C# 从 2 个表更新数据库

转载 作者:可可西里 更新时间:2023-11-01 08:27:02 25 4
gpt4 key购买 nike

使用 phpMyAdminc#

我想用 c# 更新 phpMyAdmin 中的数据库。但是,我有一个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from bucket inner join color on bucket.color_idcolor = color.idcolor' at line 1

phpMyAdmin 中,我在同一个数据库中有 2 个表:bucketcolor。在 bucket 表中,我有列:idbucketvolumecolor_idcolor。在 color 表中,我有列:idcolorname

c#中,我制作了一个windows窗体,用户可以在其中更新数据库。他们可以更新 volume(文本框)和 color(组合框),然后按“保存”按钮将更新存储到数据库中。这是我的代码的几个部分:

private string idBucket;
private string volume;
private string color;

public void TransferData(string idBucket, string volume, string color)
{
this.idBucket = idBucket;
this.volume = volume;
this.color = color;
}

MySqlConnection conector = new MySqlConnection();

public frmChangeBucket()
{
InitializeComponent();
conector.ConnectionString = "server=localhost; database=bucket; uid=root";
}

void ContentCboColor()
{
conector.Open();

MySqlCommand comand = new MySqlCommand();
comand.Connection = conector;
comand.CommandType = CommandType.Text;
comand.CommandText = "select idcolor, name from color";

MySqlDataAdapter da = new MySqlDataAdapter();
DataTable dt = new DataTable();

da.SelectCommand = comand;
da.Fill(dt);
cboColor.DisplayMember = "name";
cboColor.ValueMember = "idcolor";
cboColor.DataSource = dt;

conector.Close();
}

private void btnSave_Click(object sender, EventArgs e)
{
conector.Open();

MySqlCommand comand = new MySqlCommand();
comand.Connection = conector;
comand.CommandType = CommandType.Text;

int result = 0;

volume = txtVolume.Text;
color = cboColor.SelectedValue.ToString();

comand.CommandText = "update bucket set volume=@volume, color=@color.name, from bucket "
+ "inner join color on bucket.color_idcolor = color.idcolor "
+ "where idBucket=@idbucket";

komen.Parameters.AddWithValue("@idbucket", idBucket);
komen.Parameters.AddWithValue("@volume", volume);
komen.Parameters.AddWithValue("@color.name", color);
result += comand.ExecuteNonQuery();
comand.Parameters.Clear();

if (result > 0)
{
MessageBox.Show("You've changed " + result + " data");
}
else
{
MessageBox.Show("You haven't changed any data");
}

conector.Close();
this.Close();
}

c# 中,我只是在组合框中显示颜色 name。首先,用户可以显示一些来自数据库的数据。我使用内部联接来显示颜色 name。要将更新存储到数据库中,我是否必须再次inner join?请用正确的代码回答。谢谢你的帮助。

最佳答案

而不是尝试在 int 字段中保存文本(color=@color.name)你应该使用:

"color_idcolor = color.idcolor"

基本上:

    comand.CommandText = "update bucket set volume=@volume, color_idcolor=@color.idcolor "
+ "where idBucket=@idbucket";
comand.Parameters.AddWithValue("@idbucket", idBucket);
comand.Parameters.AddWithValue("@volume", volume);
comand.Parameters.AddWithValue("@color.idcolor", color);
result += comand.ExecuteNonQuery();
comand.Parameters.Clear();

关于c# - 使用 C# 从 2 个表更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com