如何检查列的值是否为假我想插入我的数据

时间:2018-12-05 13:37:00

标签: c# sql-server

enter image description here

如果trigger_0的最后一个值为False,我正在创建C#应用程序,请插入我的当前日期和时间,帮助我解决此问题:

        String test = DateTime.Now.ToShortDateString();
        String test1 = DateTime.Now.ToString("hh:mm");
        con.Open();
        SqlCommand cmdt = con.CreateCommand();
        cmdt.CommandType = CommandType.Text;
        cmdt.CommandText = "SELECT TOP 1 trigger_0 FROM WINDATAA  ORDER BY trigger_0 DESC ";
        cmdt.ExecuteNonQuery();
        con.Close();

3 个答案:

答案 0 :(得分:0)

我猜您正在尝试更新所有行:

<template>
    <div>
        <h1>
            Your video chat will start shortly...
        </h1>
        <br>
        <h2>
            Number of customers Ahead: {{numberOfCustomersAhead}}
        </h2>
        <br><br><br>
        <!-- <button color="error" v-on:click="startMyVisit">Start My Visit</button> -->
        <v-btn color="success" v-on:click="cancelVisit">Cancel My Video Chat</v-btn>
        <app-visit-info></app-visit-info>
    </div>
</template>

<script>
/* eslint-disable */
import visitInfo from './visitInfo';

export default {
    name: 'waitingDetails',
    components:{
        'app-visit-info': visitInfo
    },
    created(){
        console.log('created() calling method "startMyVisit()"');
        this.startMyVisit();
    },
    mounted(){
        console.log('mounted() calling method "startMyVisit()"');
        this.startMyVisit();
    },
    computed:{
        numberOfCustomersAhead(){
            return this.$store.state.visit.numberOfCustomersAhead;
        },
        getSdk(){
            return this.$store.state.sdk;
        },
        getVisit(){
            console.log('computed: getVisit');
            return this.$store.state.visit;
        }                      
    },
    watch: {
        getVisit: function(){
            if (this.getVisit !== null && this.getVisit !== '') {
                console.log('watch: getVisit !== null and is not empty!');
                startMyVisit();
            } else {
                console.log('watch: getVisit is NULL or is EMPTY');
            }
        }
    },
    methods:{
        startMyVisit(){
            if (this.getVisit !== null && this.getVisit !== '') {
                this.getSdk.visitService.launchVisitVideoChat(this.getVisit) 
                    .then((videoChatLaunched) => {
                if(videoChatLaunched !== true){
                    throw Error('problem launching the visit video chat');
                } else {
                    console.log("Visit Video Chat Launched Successfully !");
                }
                return this.getSdk.visitService.waitForVideoToStart(this.getVisit);
                }).then((visit) => {
                    this.$store.commit('setVisit', visit);
                    console.log('waitForVideoToStart... promise-->',visit);
                    return this.getSdk.visitService.waitForRepresentativeToJoinVisit(visit);
                }).then((updatedVisit) => {
                    this.$store.commit('setVisit', updatedVisit);
                    console.log('waitForRepresentativeToJoinVisit... promise-->',updatedVisit);
                    console.log('customers ahead', updatedVisit.customersAheadOfYou);
                    this.customersAheadOfYou = updatedVisit.customersAheadOfYou;
                    return this.getSdk.visitService.updateConnectionStatus(updatedVisit);
                }).then((visit) => {
                    this.$store.commit('setVisit', visit);
                    console.log('updateConnectionStatus... promise-->', visit);
                    return this.getSdk.visitService.waitForVisitToFinish(visit);
                }).then((visit) => {
                    this.$store.commit('setVisit', visit);
                    console.log('Visit has ended. waitForVisitToFinish... promise-->', visit);
                    return;
                });
            } else {
                console.log('startMyVisit() --> this.getVisit === null or is empty');
            }
        },
        cancelVisit(){
            this.getSdk.visitService.cancelVisit(this.getVisit)
            .then((visit) => {
                console.log('Cancel visit.. promise-->',visit);
            });
        }        
    }
}
</script>

如果只需要修改最后一个,则可以使用一些ID,或者提前获取最大/最后日期,并使用UPDATE WINDATAA SET [date] = CONVERT(VARCHAR(10), GETUTCDATE(), 121) ,[time] = LEFT(CAST(GETUTCDATE() AS TIME), 5) WHERE [triggger_0] = 'false' -- or [triggger_0] = 0 if boolean 子句中的值。

答案 1 :(得分:0)

只需从执行的查询中获取结果,然后查看它是否为true。如果true,则只需插入您的数据:

bool trigger = true;
SqlCommand cmdt = con.CreateCommand();
cmdt.CommandType = CommandType.Text;
var result = cmdt.ExecuteScalar();       
bool.TryParse(result, out trigger);
if (trigger == false) {
    // insert your data. For example:
   string query = "INSERT INTO dbo.FooTable (id,username) VALUES (@id,@username)"; 
   SqlCommand command = new SqlCommand(query, db.Connection);
   string preventNull = null;
   preventNull = preventNull == null ? "Is not null" : "Hey!";
   string fooUserName = null;
   fooUserName = fooUserName == null ? "Bob not null" : "Hey!";
   command.Parameters.Add("@id", preventNull );
   command.Parameters.Add("@username", fooUserName );      
   command.ExecuteNonQuery();
}

答案 2 :(得分:0)

如果我对它的理解正确,那么您想检查表中最后创建的项目的值是true还是false。如果数据库中的 time 字段描述了创建时间,则只需按日期和时间订购商品,例如

MyModel.update()

我看到的另一个选项是,如果表的ID是整数,那么您可以按ID排序项目:

SELECT TOP 1 trigger_0 FROM WINDATAA  ORDER BY date, time DESC 

然后您可以使用C#创建一条语句,并在需要时使用对象的数据