我们能否在c#winform中插入一些数据以及插入值选择查询

问题描述 投票:0回答:1

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

我的invoice表中有五列

Student_id, Amount, Fee_description, issue_date, month

我可以插入前三列的值,但其余两列为null,我不知道将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值相加?

c# sql datetime sql-insert datetimepicker
1个回答
1
投票

当然。它看起来像这样:

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为一个名为“我们可以停止使用AddWithValue了”的博客进行了很长的讨论,以讨论如何摆脱它(在这种情况下是合理的,因为它没有用于过滤),但现在我要联系的概念是:

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

SELECT 'hello' FROM table

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

SELECT @pHello FROM table

因此,您可以在白天的时间选择器(或datetime.Now或其他任何东西)中添加参数并将其归档为固定值,并且使用INSERT ... SELECT样式语句插入固定值应该做的事情

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

© www.soinside.com 2019 - 2024. All rights reserved.