2 几十上百万行,如何快速查询出表数据
答:用分页存储过程
/*
函数名称: GetRecordFromPage
函数功能: 获取指定页的数据
参数说明: @tblName 包含数据的表名
@fldName 关键字段名
@PageSize 每页记录数
@PageIndex 要获取的页码
@OrderType 排序类型, 0 - 升序, 1 - 降序
@strWhere 查询条件 (注意: 不要加 where)
*/
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
exec (@strSQL)
GO
三、数据库查询优化:
1、多态性,多种数据库兼容;
2、支持翻页,支持查询总数,页码显示;
3、能处理100万以上数据量;
答:
CREATE PROCEDURE dbo.LSP_SP_SelectElementByPage
@SelectFields varchar(200),/*要查询的字段列表*/
@Condition varchar(300),/*查询条件*/
@PageSize int =20,/*页面大小,默认为20*/
@PageNumber int =1/*页号,默认为第一页*/
/*@PageCount int out返回满足条件的总页数*/
AS
begin
declare @count int
select @count =count(*) from lsp_t_elementInfo
if(@count %@PageSize=0)
set @count = @count/@PageSize
else
set @count = @count/@PageSize +1
select @count PageCount
select IDENTITY(int,1,1) as iid,ElementName,Type into #temptable from LSP_T_ElementInfo
select * from #temptable where iid between @PageSize * (@PageNumber -1) and @PageSize * @PageNumber
end
GO
1.两个表,写查询语句,根据两个字段一个是升序,一个将序。
答:select * from a,b where a.字段1 = b.字段1 order by a.字段2 asc,b.字段2 desc
2.根据第一题,每页面显示10条记录,在第25页时怎样显示
答:
/*
函数名称: GetRecordFromPage
函数功能: 获取指定页的数据
参数说明: @tblName 包含数据的表名
@fldName 关键字段名
@PageSize 每页记录数
@PageIndex 要获取的页码
@OrderType 排序类型, 0 - 升序, 1 - 降序
@strWhere 查询条件 (注意: 不要加 where)
*/
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
exec (@strSQL)
GO
2.写出一条Sql语句: 取出表A中第31到第40记录(SQLServer, 以自动增长的ID作为主键, 注意:ID可能不是连续的。)
select top 10 * from A where id not in (select top 30 id from A)
解2: select top 10 * from A where id > (select max(id) from (select top 30 id from A )as A)
3.public class c{ public c(string a) : this() {;}; public c() {;} } 解释第一个构造函数中发生了什么?这个构造函数有什么用?
(第一个构造函数调用了第二个构造函数,这个构造函数构造了一个c对象的实例。)
4.一个长度为10000的字符串,通过随机从a-z中抽取10000个字符组成。请用c#语言编写主要程序来实现。
答:
using System.Text;
StringBuilder sb = new StringBuilder(0, 10000);
string strABC = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] ABC = strABC.Split(',');
int len = ABC.Length;
Random rd = new Random();
for (int i = 0; i < 10000; i++)
{
sb.Append(ABC[rd.Next(len)]);
}
5.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
int[] intArr=new int[100];
ArrayList myList=new ArrayList();
Random rnd=new Random();
while(myList.Count<100)
{
int num=rnd.Next(1,101);
if(!myList.Contains(num))
myList.Add(num);
}
for(int i=0;i<100;i++)
intArr[i]=(int)myList[i];
2.如何把一个Array复制到ArrayList里
答:
foreach( object o in array )arrayList.Add(o);
8.用C#写一段选择排序算法,要求用自己的编程风格。
答:private int min;
public void xuanZhe(int[] list)//选择排序
{
for (int i = 0; i < list.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < list.Length; j++)
{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
4.写一个函数计算当参数为N的值:1-2+3-4+5-6+7……+N
答:public int returnSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
int k = i;
if (i % 2 == 0)
{
k = -k;
}
sum = sum + k;
}
return sum;
}
public int returnSum1(int n)
{
int k = n;
if (n == 0)
{
return 0;
}
if (n % 2 == 0)
{
k = -k;
}
return aaa(n - 1) + k;
}
7. 某一密码仅使用K、L、M、N、O共5个字母,密码中的单词从左向右排列,密码单词必须遵循如下规则 :
(1) 密码单词的最小长度是两个字母,可以相同,也可以不同
(2) K不可能是单词的第一个字母
(3) 如果L出现,则出现次数不止一次
(4) M不能使最后一个也不能是倒数第二个字母
(5) K出现,则N就一定出现
(6) O如果是最后一个字母,则L一定出现
问题一:下列哪一个字母可以放在LO中的O后面,形成一个3个字母的密码单词?
A) K B)L C) M D) N
答案:B
问题二:如果能得到的字母是K、L、M,那么能够形成的两个字母长的密码单词的总数是多少?
A)1个 B)3个 C)6个 D)9个
答案:A
问题三:下列哪一个是单词密码?
A) KLLN B) LOML C) MLLO D)NMKO
答案:C
62-63=1 等式不成立,请移动一个数字(不可以移动减号和等于号),使得等式成立,如何移动?
答案:62移动成2的6次方
17、列出常用的使用javascript操作xml的类包
答:
XML.prototype.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
XML.prototype.InitXML=InitXML;
XML.prototype.getFirstChild=getFirstChild;
XML.prototype.getLastChild=getLastChild;
XML.prototype.getChild=getChild; // 取得节点值
XML.prototype.getNodeslength=getNodeslength; // 最得节点下的子节点的个数
XML.prototype.getNode=getNode; // 取得指定节点
XML.prototype.delNode=delNode; // 删除指定节点,如果节点相同,则删除最前面的节点.
XML.prototype.getNodeAttrib=getNodeAttrib; // 取得节点的指定属性值.
XML.prototype.InsertBeforeChild=InsertBeforeChild; // 在指定节点之前插入一个节点.
XML.prototype.InsertChild=InsertChild; // 在指定节点下插入节点.
XML.prototype.setAttrib=setAttrib; // 设置指定属性的值.
XML.prototype.setNodeValue=setNodeValue; // 设置指定节点的值.
XML.prototype.CreateNodeS=CreateNodeS; // 创建一个指定名的节点.
XML.prototype.addAttrib=addAttrib; // 为指定节点添加指定属性,并设置初值.
XML.prototype.FindString=FindString; // 在指定节点下查找字符串.
给定以下XML文件,完成算法流程图<FileSystem>
< DriverC >
<Dir DirName=”MSDOS622”>
<File FileName =” Command.com” ></File>
</Dir>
<File FileName =”MSDOS.SYS” ></File>
<File FileName =” IO.SYS” ></File>
</DriverC>
</FileSystem>
请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。
答:
void FindFile( Directory d )
{
FileOrFolders = d.GetFileOrFolders();
foreach( FileOrFolder fof in FileOrFolders )
{
if( fof is File )
You Found a file;
else if ( fof is Directory )
FindFile( fof );
}
}
6.C#代码实现,确保windows程序只有一个实例(instance)
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoid Main()
{
//防止程序多次运行
if(!OneInstance.IsFirst("GetPayInfo"))
{
MessageBox.Show ("警告:程序正在运行中! 请不要重复打开程序!可在右下角系统栏找到!","程序错误提
示:",MessageBoxButtons.OK,MessageBoxIcon.Stop);
return;
}
Application.Run(new Form1());
}
// ******************* 防止程序多次执行 **************************
publicabstractclass OneInstance
{
///<summary>
///判断程序是否正在运行
///</summary>
///<param name="appId">程序名称</param>
///<returns>如果程序是第一次运行返回True,否则返回False</returns>
publicstaticbool IsFirst(string appId)
{
bool ret=false;
if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero)
{
CreateMutex(IntPtr.Zero,0,appId);
ret=true;
}
return ret;
}
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
privatestaticextern IntPtr OpenMutex(
uint dwDesiredAccess, // access
int bInheritHandle, // inheritance option
string lpName // object name
);
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
privatestaticextern IntPtr CreateMutex(
IntPtr lpMutexAttributes, // SD
int bInitialOwner, // initial owner
string lpName // object name
);
}