.net c#将双打列表作为字符串发送到数据库

Sending a list of doubles as strings to the database
2021-09-15
  •  译文(汉语)
  •  原文(英语)

只是与下面的代码混淆.

我有像下面这样的课程

public class Counter
{       
    public Double NormalCounter { get; set; }
    public Double SATCounter { get; set; }
    public Double SUNCounter { get; set; }

}

在我的主班里,我有方法做一些计算来填补柜台

Counter CountHrs = GetBookedHours(resourceBkg, PrevEvent);
var lstExpResult = new List<string> {CountHrs.NormalCounter.ToString(),
                                     CountHrs.SATCounter.ToString(), 
                                     CountHrs.SUNCounter.ToString()};
UpdateBooking(bookingSesid, lstExpResult);

只是假设我在计数器中具有如下所示的值

普通计数器= 4

SATCounter = 10

SUNCounter = 6

我试图添加到字符串列表并更新数据库.这是正确的方法吗?或我有其他选择.

我的更新预订方法如下.

 public static bool UpdateBooking(string BookingSesid,List<string>HoursByRate)
    {
         SchedwinEntities db = new SchedwinEntities();
         string value = string.Empty;
         for (int i = 0; i < 5; i++)
         {
             string str = " ";
             if (i < HoursByRate.Count())
             {
                 str = HoursByRate[i];
                 value += str + ((char)13).ToString() + ((char)10).ToString();
             }
         }
            var query = 
            from SEVTs in db.SEVTs
            where
            SEVTs.SESID.Trim() == BookingSesid//SESID
            select SEVTs;
        foreach (var SEVTs in query)
        {
            SEVTs.USER3 = value;//value
        }
        try
        {
            db.SaveChanges();
            return true;
        }
        catch (UpdateException ex)
        {
            return false;
        }
    }
速聊1:
我的C ++还说,如果您有计数器,则应使用整数类型,不能保证cuz double保持精确值,但是同样,我们也不知道您在做什么.
速聊2:
不知道为什么我对这个问题持否定态度?
速聊3:
我的猜测是因为该问题的第一次迭代尚不清楚.(我没有投票给你,这只是我的猜测).
速聊4:
无论如何,非常感谢其他人
解决过程1

而不是传递表示doubles的字符串的列表,您应该传递键值对的列表,从中构造一个参数化的语句,并使用键值对的列表绑定参数值,如下所示:

class SqlParamBinding {
    string Name {get;set;}
    object Value {get;set;} 
}
var lstExpResult = new List<SqlParamBinding> {
    new SqlParamBinding { Name = "NormalCounter", Value = CountHrs.NormalCounter}
,   new SqlParamBinding { Name = "SATCounter", Value = CountHrs.SATCounter}
,   new SqlParamBinding { Name = "SUNCounter", Value = CountHrs.SUNCounter}
};
UpdateBooking(bookingSesid, lstExpResult);

现在lstExpResult将名称和值分开了,您的UpdateBooking代码可以将SQL表达式的格式设置为

WHERE NormalCounter=@NormalCounter AND SATCounter=@SATCounter AND ...

然后绑定@NormalCounter,@SATCounter以及@SUNCounter在传递的值lstExpResult列表.

解决过程2

如果要返回Counter类列表,则使用List而不是List.看起来您可能有一堆Counter对象可能正在更新或发送到数据库.

    Counter c = new Counter();
    c. NormalCounter = 4
    c.SATCounter = 10
    c.SunCounter = 10

    List<Counter> listCounter = new List<Counter>
    listCounter.Add(c);

代码更具可维护性和可读性.

如果一次发送一个对象,则根本不需要列表.您可以将计数器对象传递给UpdateMethod并在更新数据库时对其进行解析.

速聊1:
谢谢@opensourcelover,如果您看到我的更新方法将采用字符串列表,这就是为什么要尝试上面的选项的原因,否则您的方法是最简单,整洁的.

Just quite confused with the piece of code below.

I have class like below

public class Counter
{       
    public Double NormalCounter { get; set; }
    public Double SATCounter { get; set; }
    public Double SUNCounter { get; set; }

}

in my main class i have method to do some calculation to fill the counter

Counter CountHrs = GetBookedHours(resourceBkg, PrevEvent);
var lstExpResult = new List<string> {CountHrs.NormalCounter.ToString(),
                                     CountHrs.SATCounter.ToString(), 
                                     CountHrs.SUNCounter.ToString()};
UpdateBooking(bookingSesid, lstExpResult);

Just assume i have the value like below in the counter

NormalCounter =4

SATCounter=10

SUNCounter=6

am trying to add in to string list and update the database.is that the right way to do ? or any other options i have please.

my update booking method is below to give clear idea.

 public static bool UpdateBooking(string BookingSesid,List<string>HoursByRate)
    {
         SchedwinEntities db = new SchedwinEntities();
         string value = string.Empty;
         for (int i = 0; i < 5; i++)
         {
             string str = " ";
             if (i < HoursByRate.Count())
             {
                 str = HoursByRate[i];
                 value += str + ((char)13).ToString() + ((char)10).ToString();
             }
         }
            var query = 
            from SEVTs in db.SEVTs
            where
            SEVTs.SESID.Trim() == BookingSesid//SESID
            select SEVTs;
        foreach (var SEVTs in query)
        {
            SEVTs.USER3 = value;//value
        }
        try
        {
            db.SaveChanges();
            return true;
        }
        catch (UpdateException ex)
        {
            return false;
        }
    }
Talk1:
The c++ in me also says you should use an integer type if you have counters, cuz doubles aren't guaranteed to stay on exact values, but again, we don't know what you are doing.
Talk2:
Am not sure why i got negative for this question ?
Talk3:
My guess is because the first iteration of the question was unclear. (I didn't downvote you, it's just my guess).
Talk4:
any way thanks a lot rein and others
Solutions1

Rather than passing a list of strings that represent doubles, you should pass a list of key-value pairs, construct a parametersized statement from them, and use the list of key-value-pairs to bind parameter values, like this:

class SqlParamBinding {
    string Name {get;set;}
    object Value {get;set;} 
}
var lstExpResult = new List<SqlParamBinding> {
    new SqlParamBinding { Name = "NormalCounter", Value = CountHrs.NormalCounter}
,   new SqlParamBinding { Name = "SATCounter", Value = CountHrs.SATCounter}
,   new SqlParamBinding { Name = "SUNCounter", Value = CountHrs.SUNCounter}
};
UpdateBooking(bookingSesid, lstExpResult);

Now that lstExpResult separates names from values, your UpdateBooking code could format the SQL expression as

WHERE NormalCounter=@NormalCounter AND SATCounter=@SATCounter AND ...

and then bind @NormalCounter, @SATCounter, and @SUNCounter to the values passed in the lstExpResult list.

Solutions2

If it is going to list of Counter classes then have List instead of List. Looks like you might be having bunch of Counter objects that might be getting updated or sent to the database.

    Counter c = new Counter();
    c. NormalCounter = 4
    c.SATCounter = 10
    c.SunCounter = 10

    List<Counter> listCounter = new List<Counter>
    listCounter.Add(c);

Code is more maintainable and readable.

If you are sending one object at a time, then no need of list at all. You can pass in the counter object to your UpdateMethod and parse it while updating the database.

Talk1:
thanks @opensourcelover, if you seen my update method will take string list,that's why am trying the option above otherwise your's is the easiest and neat one.
转载于:https://stackoverflow.com/questions/15560711/sending-a-list-of-doubles-as-strings-to-the-database

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入