C# DAL添加数据的两种处理方式

时间:2022-10-16 23:41:55

方式一:

 1 /// <summary>
 2         /// 增加一条数据
 3         /// </summary>
 4         public void Add(Maticsoft.Model.TAB_BPBJ model)
 5         {
 6             StringBuilder strSql=new StringBuilder();
 7             strSql.Append("insert into TAB_BPBJ(");
 8             strSql.Append("BJID,BJBH,BJMC,BJFLID,GGXH,JSCS,GYS,BZ,JLDW,DJ,BJSL,NXHL,CFWZ)");
 9             strSql.Append(" values (");
10             strSql.Append(":BJID,:BJBH,:BJMC,:BJFLID,:GGXH,:JSCS,:GYS,:BZ,:JLDW,:DJ,:BJSL,:NXHL,:CFWZ)");
11             OracleParameter[] parameters = {
12                     new OracleParameter(":BJID", OracleType.VarChar,40),
13                     new OracleParameter(":BJBH", OracleType.VarChar,20),
14                     new OracleParameter(":BJMC", OracleType.VarChar,60),
15                     new OracleParameter(":BJFLID", OracleType.VarChar,40),
16                     new OracleParameter(":GGXH", OracleType.VarChar,60),
17                     new OracleParameter(":JSCS", OracleType.VarChar,200),
18                     new OracleParameter(":GYS", OracleType.VarChar,200),
19                     new OracleParameter(":BZ", OracleType.VarChar,200),
20                     new OracleParameter(":JLDW", OracleType.VarChar,40),
21                     new OracleParameter(":DJ", OracleType.Number,10),
22                     new OracleParameter(":BJSL", OracleType.Number,10),
23                     new OracleParameter(":NXHL", OracleType.Number,10),
24                     new OracleParameter(":CFWZ", OracleType.VarChar,40)};
25             parameters[0].Value = model.BJID;
26             parameters[1].Value = model.BJBH;
27             parameters[2].Value = model.BJMC;
28             parameters[3].Value = model.BJFLID;
29             parameters[4].Value = model.GGXH;
30             parameters[5].Value = model.JSCS;
31             parameters[6].Value = model.GYS;
32             parameters[7].Value = model.BZ;
33             parameters[8].Value = model.JLDW;
34             parameters[9].Value = model.DJ;
35             parameters[10].Value = model.BJSL;
36             parameters[11].Value = model.NXHL;
37             parameters[12].Value = model.CFWZ;
38 
39             DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
40         }

方式二:

 

 1 /// <summary>
 2         /// 增加一条数据
 3         /// </summary>
 4         public void Add(Maticsoft.Model.TAB_BPBJ model)
 5         {
 6             StringBuilder strSql=new StringBuilder();
 7             StringBuilder strSql1=new StringBuilder();
 8             StringBuilder strSql2=new StringBuilder();
 9             if (model.BJID != null)
10             {
11                 strSql1.Append("BJID,");
12                 strSql2.Append("'"+model.BJID+"',");
13             }
14             if (model.BJBH != null)
15             {
16                 strSql1.Append("BJBH,");
17                 strSql2.Append("'"+model.BJBH+"',");
18             }
19             if (model.BJMC != null)
20             {
21                 strSql1.Append("BJMC,");
22                 strSql2.Append("'"+model.BJMC+"',");
23             }
24             if (model.BJFLID != null)
25             {
26                 strSql1.Append("BJFLID,");
27                 strSql2.Append("'"+model.BJFLID+"',");
28             }
29             if (model.GGXH != null)
30             {
31                 strSql1.Append("GGXH,");
32                 strSql2.Append("'"+model.GGXH+"',");
33             }
34             if (model.JSCS != null)
35             {
36                 strSql1.Append("JSCS,");
37                 strSql2.Append("'"+model.JSCS+"',");
38             }
39             if (model.GYS != null)
40             {
41                 strSql1.Append("GYS,");
42                 strSql2.Append("'"+model.GYS+"',");
43             }
44             if (model.BZ != null)
45             {
46                 strSql1.Append("BZ,");
47                 strSql2.Append("'"+model.BZ+"',");
48             }
49             if (model.JLDW != null)
50             {
51                 strSql1.Append("JLDW,");
52                 strSql2.Append("'"+model.JLDW+"',");
53             }
54             if (model.DJ != null)
55             {
56                 strSql1.Append("DJ,");
57                 strSql2.Append(""+model.DJ+",");
58             }
59             if (model.BJSL != null)
60             {
61                 strSql1.Append("BJSL,");
62                 strSql2.Append(""+model.BJSL+",");
63             }
64             if (model.NXHL != null)
65             {
66                 strSql1.Append("NXHL,");
67                 strSql2.Append(""+model.NXHL+",");
68             }
69             if (model.CFWZ != null)
70             {
71                 strSql1.Append("CFWZ,");
72                 strSql2.Append("'"+model.CFWZ+"',");
73             }
74             strSql.Append("insert into TAB_BPBJ(");
75             strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
76             strSql.Append(")");
77             strSql.Append(" values (");
78             strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
79             strSql.Append(")");
80             DbHelperOra.ExecuteSql(strSql.ToString());
81         }

 

区别:第一种方式参数不能为空,第二种方式参数可以为空