表名称 04.04.2016
===========================
id 3
name asd
co 12
price 32
st 4
表名称 05.04.2016
=======================
id 3 Same
name asd Same
co <--- FROM old one ST= HERE
------------------
price 32 Same`enter code here`
st Null <--- I will write here later
Table-04.04.2016 是旧表
我想打开新表并将旧表“ST”列复制到新表“CO”
但我也需要匹配 id
private void Bsayim_Click(object sender, EventArgs e)
{
MySqlCommand cmdDatabase = new MySqlCommand("CREATE TABLE IF NOT EXISTS `" + tarihbugun.Text + "` (" +
"`id` INT AUTO_INCREMENT," +
"`urun` VARCHAR(255)," +
"`gelen` VARCHAR(255)," +
"`fiyat` VARCHAR(255)," +
"`kalan` VARCHAR(255), " +
"`giden` VARCHAR(255), " +
"`maliyet` VARCHAR(255), " +
"PRIMARY KEY(id));", conn);
conn.Open();
cmdDatabase.ExecuteNonQuery();
cmdDatabase.Dispose();
conn.Close();
datagird();
if (MessageBox.Show("message", "new day", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
MySqlCommand sqlCmd1 = new MySqlCommand();
try
{
sqlCmd1.Connection = conn;
sqlCmd1.CommandType = CommandType.Text;
sqlCmd1.CommandText = "INSERT INTO `" + date.Text + "`(`id`,`product`, `pprice`) SELECT `aid`, `aproduct`, `aprice` FROM Alcol";
MySqlDataAdapter sda = new MySqlDataAdapter(sqlCmd1);
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
MessageBox.Show("ok","ok",MessageBoxButtons.OK,MessageBoxIcon.Information);
我使用这些代码打开表 05.04.2016。现在如何将“ST”从旧表复制到新表“CO”
最佳答案
我找到了它,我希望这段代码可以帮助别人。
string Query = "UPDATE `11.04.2016` AS t1 INNER JOIN `10.04.2016` AS t2 ON t1.id = t2.id SET t1.xx = t2.yy";
//This is MySqlConnection here i have created the object and pass my connection string.
// MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand cmd2 = new MySqlCommand(Query, conn);
MySqlDataReader MyReader2;
conn.Open();
MyReader2 = cmd2.ExecuteReader();
MessageBox.Show("Data Updated");
while (MyReader2.Read())
更新表名
AS t1 INNER JOIN 旧表
AS t2 ON t1.id = t2.id SET t1.Columnname = t2.othercolumnname
关于c# - 复制表也匹配 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36528229/