我们可以在 c# winform 中插入一些数据以及插入值 select 查询吗

Can we insert some data along with insert value select query in c# winform

我想插入日期和月份(在两个日期时间选择器中)以及插入值 select 查询。

我的 invoice table

中有五列
Student_id, Amount, Fee_description, issue_date, month

我可以插入前三列的值,但其余两列为空,我不知道将 datetimepicker 值放在哪里??

我在表单的设计视图中使用日期和月份的数据时间选择器

insert into invoice (Student_id, Amount, Fee_description, issue_date, month)
    select 
        student.Student_id, 
        Fee.Fee_Amount, Fee.fee_type
    from 
        student
    join 
        parent on student.father_nic = parent.father_nic
    join 
        allocate_class on student.Student_id = allocate_class.Student_id
    join 
        class on class.class_id = allocate_class.class_id
    join 
        session on allocate_class.session_id = session.session_id
    join 
        Fee on class.class_id = Fee.class_id
    where 
        session.session_id = 1
        and Fee.fee_type = 'Tution Fee'
        and student.status = 'active'

在上面的查询中在哪里添加那两个 datetimpicker 值?

当然可以。它看起来像这样:

var cmd = new SqlCommand("insert into invoice (
    Student_id,
Amount,
Fee_description,
issue_date,
month
)
select student.Student_id
 , Fee.Fee_Amount
 , Fee.fee_type

 , @issDat
 , @mon

  from ...", "conn str here");


cmd.Parameters.AddWithValue("@issDat", issueDateDateTimePicker.Value);
cmd.Parameters.AddWithValue("@mon", monthDateTimePicker.Value);

我使用 AddWithValue 快速解释了这个概念 - google 一个名为 "can we stop using AddWithValue already" 的博客就如何摆脱它进行了长时间的讨论(在这种情况下这是合理的,因为它不是用于过滤),但现在我试图联系的概念是:

sql 语句可以从您那里获取固定值:

SELECT 'hello' FROM table

它也可以采用您提供的固定参数:

SELECT @pHello FROM table

因此,添加参数并使用白天时间选择器(或 datetime.Now 或其他)的固定值对它们进行归档是可以的,您应该如何使用 INSERT ... SELECT 样式声明

旁注,尚不清楚月份和发行日期是否相关,但如果它们是同一日期,则您无需将其存储两次 - 您可以随时从日期中提取月份.