c# - 复制表也匹配 id

标签 c# mysql

表名称 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/

相关文章:

php - 使用未知数量的参数查询数据库

mysql - 仅选择每个用户用时最短的条目

c# - Excel 如何识别日期单元格以及如何使用 OpenXml 和 C# 执行相同操作

c# - 如何使用 EF4 Code-First 一次创建两个关联实体

php - Doctrine\DBAL\Driver\PDOException SQLSTATE[HY000] [2006] MySQL 服务器已消失

mysql - mySQL 中的 Groupby Sum?

MySQL - WHERE ATD 与 STD 的差值在负 10 分钟内

c# - 如何使用 jQuery 复制带有下拉列表的表行

c# - 如何在终端中运行命令并捕获输出

c# - Devexpress GridView 行数