首页 新闻 搜索 专区 学院

判断数据库表中是否为空,空则插入

0
[已解决问题] 解决于 2013-01-28 14:20

        string sqlBudget = "select b.* from BudgetApplicationAndPayout as b join WXFYLX as w on b.wid=w.id where w.ssnf='2013'";
        DataTable budget = this.CommonBLL.getResult(sqlBudget);

        if (budget.Rows.Count == 0)
        {
            insSQL = new System.Text.StringBuilder();
            insSQL.Append("insert into BudgetApplicationAndPayout (wid) select id from WXFYLX where  WXFYLX.ssnf='2013'");
            insSQL.Append(@"update BudgetApplicationAndPayout set firstjdje=0.00,secondlyjdje=0.00,thirdjdje=0.00,fourjdje=0.00
   ,jdjeTotal=0.00,Janbk=0.00,Febbk=0.00,Marbk=0.00,Aprbk=0.00,Maybk=0.00,Junbk=0.00,Julbk=0.00,Augbk=0.00,Sepbk=0.00
             ,Octbk=0.00,Novbk=0.00,Decbk=0.00,yfjeTotal=0.00");

大家帮看一下怎么不行呢。

Ole_Yao的主页 Ole_Yao | 菜鸟二级 | 园豆:236
提问于:2013-01-28 13:28
< >
分享
最佳答案
0

1、 if (budget== null||(budget!= null&&budget.Rows.Count<=0){}

2、你这样的代码可能会出现并发问题,所以可以合并的的查询和插入语法(SQL效率也比较高)

select count(*) from BudgetApplicationAndPayout as b join WXFYLX as w on b.wid=w.id where w.ssnf='2013';
IF @@ROWCOUNT = 0
Begin
     insert into BudgetApplicationAndPayout (wid) select id from WXFYLX where  WXFYLX.ssnf='2013';
update BudgetApplicationAndPayout set firstjdje=0.00,secondlyjdje=0.00,thirdjdje=0.00,fourjdje=0.00,jdjeTotal=0.00,Janbk=0.00,Febbk=0.00,Marbk=0.00,Aprbk=0.00,Maybk=0.00,Junbk=0.00,Julbk=0.00,Augbk=0.00,Sepbk=0.00,Octbk=0.00,Novbk=0.00,Decbk=0.00,yfjeTotal=0.00;
end

 

            
奖励园豆:5
滴答的雨 | 老鸟四级 |园豆:3690 | 2013-01-28 13:49

另外你上面的代码中。

insSQL.Append("insert into BudgetApplicationAndPayout (wid) select id from WXFYLX where  WXFYLX.ssnf='2013'");    SQL语句语句的结尾没有分毫(;)

滴答的雨 | 园豆:3690 (老鸟四级) | 2013-01-28 13:54
其他回答(2)
0

if(budget.Rows.Count <=0)

count属性返回的是 数字,怎么可能跟null比较呢,你现在的if语句永远都不执行,换成上面的判断

chenping2008 | 园豆:9836 (大侠五级) | 2013-01-28 13:42

我那里是复制错了,换成if(budget.Rows.Count <=0)还是不行

支持(0) 反对(0) Ole_Yao | 园豆:236 (菜鸟二级) | 2013-01-28 13:49

@Ole_Yao: F10调试下,应该是有具体的错误,或者程序的运行情况不跟要的逻辑相同

支持(0) 反对(0) chenping2008 | 园豆:9836 (大侠五级) | 2013-01-28 13:50
0

楼上正解

jerry-Tom | 园豆:4077 (老鸟四级) | 2013-01-28 14:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册