如何将数据从许多PC保存到一个表SQL [英] How to save data from many PC to one table SQL

查看:68
本文介绍了如何将数据从许多PC保存到一个表SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些数据从许多PC保存到一个表中。但是数据有一个手动填充的索引(ID)和先前数据(ID)的顺序,所以我先在数据库上检查它然后递增它。问题是如果多个PC /设备一起保存,它将保存相同结果。如何解决这个问题?



我的尝试:



这是查询搜索使用的最后一个ID:

I want to save some data to one table from many PC. But the data have a index(ID) that fill manually and order from previous data(ID), so I check it first on database then increment it.The problem is If more than one PC/device save together, it will save with same result. How to solve this problem?

What I have tried:

This is query for search the last ID that used:

public DataRow GetPlayerIDByClient(object clientPK)
       {
           DataRow result = null;

           if (clientPK != null)
           {
               IDbDataParameter clientFKParam = this.CreateParameter(csPlayerNamesEntity.Names.ClientFK, clientPK);

               string selectCommand = string.Format("SELECT TOP 1 * FROM {0} WHERE {1} = {3} AND " +
                                                       "({2} LIKE 'G%' or {2} LIKE 'g%') ORDER BY {4} DESC",
                                                       csPlayerNamesEntity.Names.TableName, //0
                                                       csPlayerNamesEntity.Names.ClientFK, //1
                                                       csPlayerNamesEntity.Names.ID,//2
                                                       clientFKParam,//3
                                                       csPlayerNamesEntity.Names.PK); //4

               DataTable dt = this.ExecReaderDataTable(selectCommand, clientFKParam);

               if (dt != null && dt.Rows.Count > 0)
               {
                   result = dt.Rows[0];
               }
           }

           return result;
       }

推荐答案

如果按如下方式定义ID列,它将自动递增:

If you define your ID column as follows, it will be incremented automatically:
ID int IDENTITY(1,1) PRIMARY KEY

您还可以设置初始值,请参见此处的示例:身份栏 - 简单谈话 [ ^ ]



如果这不是你想要的,你可以考虑在插件上写一个触发器来做你想要的。

You can also set an initial value, see example here: Identity Columns - Simple Talk[^]

If that is not what you want, you could consider writing a trigger on insert that does what you want.


这篇关于如何将数据从许多PC保存到一个表SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆