
理论上,要针对所有的表各写一条SQL,SQL中要列出所有的字符类字段,例如:
SELECT FROM taba WHERE fielda LIKE '%abc%' OR fieldB LIKE '%abc%' ……;
SELECT FROM tabb WHERE fielda LIKE '%abc%' OR fieldB LIKE '%abc%' ……;
写这样的SQL非常痛苦,看你是用的什么数据库,许多数据库系统有系统字典,就是在数据库里面的某些表中存放着所有的数据库、表、字段的名称和类型,那样可以通过那些信息编写出自动生成前面的SQL语句的SQL语句。
另外,一般的数据库都是存放在文件中或者磁盘上,可能利用全盘搜索软件搜索磁盘上特定的字符串,比上述SQL语句要快得多,只是搜索到了,要判断数据库行有一些麻烦。
DATEADD
在向指定日期加上一段时间的基础上,返回新的 datetime 值。
语法
DATEADD ( datepart , number, date )
参数
datepart
是规定应向日期的哪一部分返回新值的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。
日期部分 缩写
Year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms
number
是用来增加 datepart 的值。如果指定一个不是整数的值,则将废弃此值的小数部分。例如,如果为 datepart 指定 day,为 number 指定 175,则 date 将增加 1。
date
是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。有关指定日期的更多信息,请参见 datetime 和 smalldatetime。
如果您只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049(默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。
返回类型
返回 datetime,但如果 date 参数是 smalldatetime,返回 smalldatetime。
示例
此示例打印出 pubs 数据库中标题的时间结构的列表。此时间结构表示当前发布日期加上 21 天。
USE pubs
GO
SELECT DATEADD(day, 21, pubdate) AS timeframe
FROM titles
GO
下面是结果集:
timeframe
---------------------------
Jul 3 1991 12:00AM
Jun 30 1991 12:00AM
Jul 21 1991 12:00AM
Jul 13 1991 12:00AM
Jun 30 1991 12:00AM
Jul 9 1991 12:00AM
Mar 14 1997 5:09PM
Jul 21 1991 12:00AM
Jul 3 1994 12:00AM
Mar 14 1997 5:09PM
Nov 11 1991 12:00AM
Jul 6 1991 12:00AM
Oct 26 1991 12:00AM
Jul 3 1991 12:00AM
Jul 3 1991 12:00AM
Nov 11 1991 12:00AM
Jul 3 1991 12:00AM
Jul 3 1991 12:00AM
(18 row(s) affected)
多查查帮助。
DataReader 的默认行为是在整个数据行可用时立即以行的形式加载传入数据 但是 对于二进制大对象 (BLOB) 则需要进行不同的处理 因为它们可能包含数十亿字节的数据 而单个行中无法包含如此多的数据 Command ExecuteReader 方法具有一个重载 它将采用 CommandBehavior 参数来修改 DataReader 的默认行为 您可以将 CommandBehavior SequentialAccess 传递到 ExecuteReader 方法来修改 DataReader 的默认行为 以便让 DataReader 按照顺序在接收到数据时立即将其加载 而不是加载数据行 这是加载 BLOB 或其他大数据结构的理想方案 在将 DataReader 设置为使用 SequentialAccess 时 务必要注意访问所返回字段的顺序 DataReader 的默认行为是在整个行可用时立即加载该行 这使您能够在读取下一行之前按任何顺序访问所返回的字段 但是 当使用 SequentialAccess 时 必须按顺序访问由 DataReader 返回的不同字段 例如 如果查询返回三个列 其中第三列是 BLOB 则必须在访问第三个字段中的 BLOB 数据之前返回第一个和第二个字段的值 如果在访问第一个或第二个字段之前访问第三个字段 则第一个和第二个字段值将不再可用 这是因为 SequentialAccess 已修改 DataReader 使其按顺序返回数据 当 DataReader 已经读取超过特定数据时 该数据将不可用 当访问 BLOB 字段中的数据时 请使用 DataReader 的 GetBytes 类型化访问器 该访问器将使用二进制数据填充 byte 数组 您可以指定要返回的特定数据缓冲区大小以及从返回的数据中读取的第一个字节的起始位置 GetBytes 将返回 long 值 它表示所返回的字节数 如果向 GetBytes 传递空的 byte 数组 所返回的长值将是 BLOB 中字节的总数 您可以选择将字节数组中的某索引指定为所读取数据的起始位置 以下示例从 Microsoft SQL Server 中的 pubs 示例数据库中返回发行者 ID 和徽标 发行者 ID (pub_id) 是字符字段 而徽标则是图形 即 BLOB 请注意 由于必须按顺序访问字段 所以将在访问徽标之前访问当前数据行的发行者 ID [Visual Basic]Dim pubsConn As SqlConnection = New SqlConnection(Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;)Dim logoCMD As SqlCommand = New SqlCommand(SELECT pub_id logo FROM pub_info pubsConn)Dim fs As FileStream Writes the BLOB to a file ( bmp) Dim bw As BinaryWriter Streams the binary data to the FileStream object Dim bufferSize As Integer = The size of the BLOB buffer Dim outbyte(bufferSize ) As Byte The BLOB byte() buffer to be filled by GetBytes Dim retval As Long The bytes returned from GetBytes Dim startIndex As Long = The starting position in the BLOB output Dim pub_id As String = The publisher id to use in the file name Open the connection and read data into the DataReader pubsConn Open()Dim myReader As SqlDataReader = logoCMD ExecuteReader(CommandBehavior SequentialAccess)Do While myReader Read() Get the publisher id which must occur before getting the logo pub_id = myReader GetString( ) Create a file to hold the output fs = New FileStream(logo & pub_id & bmp FileMode OpenOrCreate FileAccess Write)bw = New BinaryWriter(fs) Reset the starting byte for a new BLOB startIndex = Read bytes into outbyte() and retain the number of bytes returned retval = myReader GetBytes( startIndex outbyte bufferSize) Continue reading and writing while there are bytes beyond the size of the buffer Do While retval = bufferSizebw Write(outbyte)bw Flush() Reposition the start index to the end of the last buffer and fill the buffer startIndex = startIndex + bufferSizeretval = myReader GetBytes( startIndex outbyte bufferSize)Loop Write the remaining buffer bw Write(outbyte)bw Flush() Close the output file bw Close()fs Close()Loop Close the reader and the connection myReader Close()pubsConn Close()[C#]SqlConnection pubsConn = new SqlConnection(Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;);SqlCommand logoCMD = new SqlCommand(SELECT pub_id logo FROM pub_info pubsConn);FileStream fs; // Writes the BLOB to a file ( bmp) BinaryWriter bw; // Streams the BLOB to the FileStream object int bufferSize = ; // Size of the BLOB buffer byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes long retval; // The bytes returned from GetBytes long startIndex = ; // The starting position in the BLOB output string pub_id = ; // The publisher id to use in the file name // Open the connection and read data into the DataReader pubsConn Open();SqlDataReader myReader = logoCMD ExecuteReader(CommandBehavior SequentialAccess);while (myReader Read()){// Get the publisher id which must occur before getting the logo pub_id = myReader GetString( );// Create a file to hold the output fs = new FileStream(logo + pub_id + bmp FileMode OpenOrCreate FileAccess Write);bw = new BinaryWriter(fs);// Reset the starting byte for the new BLOB startIndex = ;// Read the bytes into outbyte[] and retain the number of bytes returned retval = myReader GetBytes( startIndex outbyte bufferSize);// Continue reading and writing while there are bytes beyond the size of the buffer while (retval == bufferSize){bw Write(outbyte);bw Flush();// Reposition the start index to the end of the last buffer and fill the buffer startIndex+= bufferSize;retval = myReader GetBytes( startIndex outbyte bufferSize);}// Write the remaining buffer bw Write(outbyte);bw Flush();// Close the output file bw Close();fs Close();}// Close the reader and the connection myReader Close();pubsConn Close(); lishixinzhi/Article/program/net/201311/12026
在ASP NET 中 使用了一种在运行时解析为连接字符串值的新的声明性表达式语法 按名称引用数据库连接字符串 连接字符串本身存储在 nfig 文件中的 <connectionStrings> 配置节下面 以便易于在单个位置为应用程序中的所有页进行维护
范例程序代码如下
<xml version= >
<configuration>
<connectionStrings>
<add name= Pubs connectionString= Server=localhost; Integrated Security=True;Database=pubs;Persist Security Info=True providerName= System Data SqlClient />
<add name= Northwind connectionString= Server=localhost; Integrated Security=True;Database=Northwind;Persist Security Info=True providerName= System Data SqlClient />
</connectionStrings>
<system web>
<pages styleSheetTheme= Default />
</system web>
</configuration>
程序代码说明 在上述范例的程序代码中 我们在Web Config文件中的<connectionStrings> 配置节点下面设置了两个数据库连接字符串 分别指向pubs和Northwind两个示例数据库 注意 在 中引进了数据源控件 例如SqlDataSource 控件 我们可以将SqlDataSource 控件的 ConnectionString 属性被设置为表达式 <%$ ConnectionStrings:Pubs %> 该表达式在运行时由 ASP NET 分析器解析为连接字符串 还可以为SqlDataSource 的 ProviderName 属性指定一个表达式 例如 <%$ ConnectionStrings:Pubs ProviderName %> 其具体的用法和新特征将在以后的章节进行详细的介绍 现在有个基础的了解即可 当然 我们也可以用下面的方式从配置文件直接读取数据库连接字符串 首先我们需要引用using System Web Configuration命名空间 该命名空间包含用于设置 ASP NET 配置的类 string connectionString =ConfigurationManager ConnectionStrings[ Northwind ] ConnectionString;
lishixinzhi/Article/program/ASP/201311/21703
如果我们想存储一个由人组成的列表及人们的详细信息,我们是使用“Person”、“Persons”、“People”还是“Peoples”呢有些人会用“People”,有些人会用“Person”,其他人或人们会用“Peoples”或“Persons”。
相关标准的规定是不进行复数化, 因为在一个表中,我们存储的是一组实体,我们按照该实体对表进行命名,所以如果我们想要在一个单独的实体或表中存储一个或更多的人, 我们就需要将他或他们存储在“Person”表中。
如果我们坚持这样做,那么它会使其他情况变得更简单,并使我们不再需要考虑如何复数化一个单词,例如,我曾看到将hierarchy复数化为"hierarcys"的。
需要证据来支持你和同事之间的争论吗
如果我们看一下由E F Codd撰写的“大型共享数据库的数据关系模型”论文, 我们就会发现基本上是他发明了关系型数据库,他给出的例子是使用单数形式(供应商和组件)。
如果我们再看看ISO关于命名的标准(11179-5: 命名和识别原则),就会发现它也规定了单数名称应该“名词只以单数形式使用”。
最后,如果我们看看微软的例子,我们就可以看到有些困惑是从何而来!
旧的“pubs(示例数据库)”例子(>
以上就是关于对整个数据库进行搜索的方法全部的内容,包括:对整个数据库进行搜索的方法、数据库中函数DATAADD需要哪些参数、C#中读取数据库中Image数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)