62,041
社区成员
发帖
与我相关
我的任务
分享
public partial class _Default : System.Web.UI.Page
{
//1000条数据 包括ID 姓名 部门名称
//现在要插入数据库 检查如果数据库中有此记录 那么进行update 如果没有记录insert
//无论是update 还是insert 都要判断这个部门在上面的树结构中是否存在
//问题:
//判断1 当用户的部门名称在数据库中存在的时候 直接判断
//判断2 当用户的部门为Microsoft BFSI 那么就要截取BFSI。来判断 那么如何判断要截取呢?
//判断3 当用户的部门为BFS 那如何去判断BFS是属于BFSI呢?
//效率:
//针对判断1: 上面直接判断的时候我打算用哈希表匹配 不知效率如何 是否有更快的
//针对判断2: 没想出来
//针对判断3: 没想出来
//假设上面3个问题都解决了
//在nhibernate里 更新Merge的时候 是用Nhibernate的Merge好呢
//还是用拼SQL好呢?(ORACLE) 貌似ORACLE 在对批量操作这方面做的不是很好。
//目的 想求一个高效的方法来完成这一个操作 从判断部门 批量Merge到数据库
//代码如下:
public static List<string> DepartMentList = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
DepartMentList.Add("Microsoft");
DepartMentList.Add("Microsoft BFSI");
DepartMentList.Add("BFSI");
DepartMentList.Add("IS");
DepartMentList.Add("MFG");
DepartMentList.Add("Microsoft GM");
DepartMentList.Add("MFG");
DepartMentList.Add("Microsoft Testing");
DepartMentList.Add("PE");
DataTable Table_Info = new DataTable();
DataColumn dc = null;
dc = Table_Info.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dc.AllowDBNull = false;
dc = Table_Info.Columns.Add("EnglishName", Type.GetType("System.String"));
dc = Table_Info.Columns.Add("ChineseName", Type.GetType("System.String"));
dc = Table_Info.Columns.Add("Department", Type.GetType("System.String"));
for (int i = 0; i < 1000; i++)
{
DataRow dr = Table_Info.NewRow();
dr[1] = RndStr(5, RndType.Char);
dr[2] = RndStr(3, RndType.Str);
dr[3] = EndDepartMent();
}
}
public enum RndType
{
Char, Number, Both, Str
};
public static string RndStr(int Len, RndType type)
{
string s = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,啊,的,快,乐,安,徽,省,好,上,看,的,大,佬,万,科,阿,萨,德";
string[] Chars = s.Split(',');
Random Rnd = new Random();
int Start = 0, End = 0;
switch (type)
{
case RndType.Char:
Start = 0;
End = 26;
break;
case RndType.Str:
Start = 26;
End = 44;
break;
}
s = " ";
for (int i = 0; i < Len; i++)
{
s += Chars[Rnd.Next(Start, End)];
System.Threading.Thread.Sleep(5);
}
return s;
}
public static string EndDepartMent()
{
Random Rnd = new Random();
return DepartMentList[Rnd.Next(1, DepartMentList.Count)];
}
}
--假设数据库中有三个部门
declare @department table (departmentid int,departmentName varchar(9))
insert into @department
select 1,'Microsoft' union all
select 2,'BFSI' union all
select 3,'MFG'
--select * from @department
--假设现在每个部门只有1个人
declare @staff table(userid int identity,username varchar(10),departmentid int)
insert into @staff
select '张三',1 union all
select '李四',2 union all
select '王五',3
--select * from @staff
--假设外面又来了100人(暂时只模拟4条)
declare @table table (ID int,姓名 varchar(2),部门名称 varchar(15))
insert into @table
select 1,'a1','Microsoft1' union all
select 2,'a2','BFSIAS' union all
select 3,'a3','Microsoft2' union all
select 4,'a4','ABC'
--select * from @table
insert into @staff
select * from
(
select 姓名,(select top 1 departmentid from @department
where charindex(departmentName,部门名称)>0) as did from @table
) aa
where did is not null --这是只匹配上的
--把where 条件改为 where did is null 就是进来的部门不存在的
--相当于把进来的1000条数据直接分为2个部分了,这样处理起来方便一些,不知道我理解的对不对。
select * from @staff
--插入后的结果:
/*
userid username departmentid
----------- ---------- ------------
1 张三 1
2 李四 2
3 王五 3
4 a1 1
5 a2 2
6 a3 1
*/
--假设数据库中有三个部门
declare @department table (departmentid int,departmentName varchar(9))
insert into @department
select 1,'Microsoft' union all
select 2,'BFSI' union all
select 3,'MFG'
select * from @department
--假设现在每个部门只有1个人
declare @staff table(userid int identity,username varchar(10),departmentid int)
insert into @staff
select '张三',1 union all
select '李四',2 union all
select '王五',3
select * from @staff
--假设外面又来了100人(暂时只模拟4条)
declare @table table (ID int,姓名 varchar(2),部门名称 varchar(15))
insert into @table
select 1,'a1','Microsoft1' union all
select 2,'a2','BFSIAS' union all
select 3,'a3','Microsoft2' union all
select 4,'a4','BFSICF'
select * from @table
/*
要把这100个人批量插入员工表中,然后部门根据名称匹配,是这个意思吗?
如果进来的人员的部门是ABC,匹配不到,
则该人员不插入,还是插入后,自动创建部门到部门表?
*/
declare @table table (ID int,姓名 varchar(4),部门名称 varchar(9),c4 varchar(4))
insert into @table
select 1,'张三','Microsoft','BFSI' union all
select 2,'李四','BFSI',null union all
select 3,'王五','Microsoft',null union all
select 4,'刘六','BFSI',null
--...
select * from @table
/*
ID 姓名 部门名称 c4
----------- ---- --------- ----
1 张三 Microsoft BFSI
2 李四 BFSI NULL
3 王五 Microsoft NULL
4 刘六 BFSI NULL
*/