【问题标题】:how to do a c# insert with a select values in a select from database如何在从数据库中选择时使用选择值进行 c# 插入
【发布时间】:2021-12-02 05:29:13
【问题描述】:

我正在尝试让我的选择值进入我的插入但我不知道如何制作这个循环。目前,我的选择返回超过 1 个值。但他只插入第一个。有人可以帮我解决这个循环吗???

            {
                string cs = @"Data Source=xxxx";
                SqlConnection cn = new SqlConnection(cs);
                cn.Open();
                Console.WriteLine("established connection");

                string query = @"SELECT a.idfaturamento,
                                       a.id_capa,
                                       a.pagamentodata,
                                       a.mensal,
                                       b.id_status
                                FROM   capas_faturamentos AS a
                                       INNER JOIN capas AS b
                                               ON a.id_capa = b.id_capa
                                WHERE  b.id_status = 4
                                       AND a.mensal = 1
                                       AND Month(a.pagamentodata) != Month(Getdate()) ";

                SqlCommand cmd1 = new SqlCommand(query, cn);
                SqlDataReader r = cmd1.ExecuteReader();

                DateTime pagamentoData = DateTime.Today;
                int id_Capa = 0;
                bool temFaturamentoMensal = false;

                while (r.Read() == true)
                {
                    int idFaturamento = r.GetInt32(0);
                    id_Capa = r.GetInt32(1);
                    pagamentoData = r.GetDateTime(2);

                    Console.WriteLine("ID FATURAMENTO: {0}\t ID CAPA:{1}\t DATA:{2}\t",
                        idFaturamento, id_Capa, pagamentoData);
                    temFaturamentoMensal = true;
                }
                cn.Close();

                Console.WriteLine("___________________");

                cn.Open();
                if (temFaturamentoMensal)
                { 
                    string query2 = @"insert into capas_faturamentos values ('"+ id_Capa +"','" + pagamentoData.ToString("yyyy/MM/dd") + "', '" + pagamentoData.ToString("yyyy/MM/dd") + "', '" + pagamentoData.ToString("yyyy/MM/dd") + "', '" + pagamentoData.ToString("yyyy/MM/dd") + "', '" + pagamentoData.ToString("yyyy/MM/dd") + "', '" + pagamentoData.ToString("yyyy/MM/dd") + "','0','0','0','0','0','0', NULL, NULL, NULL, NULL, NULL, NULL, '1')";
                    Console.WriteLine(query2);
                    SqlCommand cmd = new SqlCommand(query2, cn);
                    int result = cmd.ExecuteNonQuery();
                    Console.WriteLine(result + " record/s insert in table capas_faturamentos");
                }
                cn.Close();```

I need help creating this loop.

【问题讨论】:

  • 使用计数ID然后插入循环内
  • AND Month(a.pagamentodata) != Month(Getdate()) 明年 10 月,您(或其他人)可能会因为它发生出乎意料的事情而感到不快。

标签: c# .net sql-server select insert


【解决方案1】:

对于您的插入,您不需要循环

只运行

insert into capas_faturamentos 
SELECT 
       a.id_capa,
       a.pagamentodata,
       a.pagamentodata,
       a.pagamentodata,
       a.pagamentodata,
       a.pagamentodata,
       a.pagamentodata,
       '0','0','0','0','0','0', NULL, NULL, NULL, NULL, NULL, NULL, '1'    
FROM   capas_faturamentos AS a
       INNER JOIN capas AS b
               ON a.id_capa = b.id_capa
WHERE  b.id_status = 4
       AND a.mensal = 1
       AND Month(a.pagamentodata) != Month(Getdate()) 

如果您想完全使用循环,请使用准备好的语句,如How do multi rows insert with MySqlCommand and prepare statement?(#C)

【讨论】:

    【解决方案2】:

    你不需要任何循环。将所有内容放在一个 sql 脚本中

    string query = @"
    insert into capas_faturamentos 
    SELECT a.idfaturamento,
             a.id_capa,
             a.pagamentodata,
               a.pagamentodata,
                a.pagamentodata,
               a.pagamentodata, 
                 a.pagamentodata,
               a.pagamentodata, 
                '0','0','0','0','0','0', NULL, NULL, NULL, NULL, NULL, NULL, '1' 
      FROM   capas_faturamentos AS a
             INNER JOIN capas AS b
                     ON a.id_capa = b.id_capa
      WHERE  b.id_status = 4
             AND a.mensal = 1
             AND Month(a.pagamentodata) != Month(Getdate()) ";
    

    和代码

     string cs = @"Data Source=xxxx";
    var myConnection = new MySqlConnection(cs);
    MySqlCommand myCommand = new MySqlCommand(query, myConnection);
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
    

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多