ExecuteReader: CommandText 属性尚未初始化,线等,立结!!!急!急!

tomsoncat 2003-09-29 04:03:51
ICollection CreateSource(string sqlSel)
{
SqlConnection conn=new SqlConnection(Application["conn"].ToString());
int StartIndex;
StartIndex = CurrentPage*PageSize;
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlSel,conn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds,StartIndex,PageSize,"files");//这行报错
return ds.Tables["files"].DefaultView;
}

这是datagrid的数据绑定,自己加分页,以前没问题,今天突然抱错了。我没有用SqlCommand,怎么回这样抱错?求救。
...全文
35 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LiveIsLive 2003-09-29
  • 打赏
  • 举报
回复
你是哪里出错了啊?创建DataSet不用"ExecuteReader"啊,这个是创建SqlDataReader用的啊。
tomsoncat 2003-09-29
  • 打赏
  • 举报
回复
是晕了,接分。
tomsoncat 2003-09-29
  • 打赏
  • 举报
回复
不空,判断过了。
LiveIsLive 2003-09-29
  • 打赏
  • 举报
回复
是不是你的SqlSel是一个空值啊?
.net数据访问类 SQL Helper 介绍 摘要:Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以及向 SQL Server 数据库发出 SQL 文本命令。它返回 SqlDataReader、DataSet 和 XmlReader 对象。您可以在自己的 .NET 应用程序中将其作为构造块来使用,以减少需要创建、测试和维护的自定义代码的数量。您可以下载完整的 C# 和 Visual Basic .NET 源代码以及综合文档。 简介 您是否正在从事 .NET 应用程序数据访问代码的设计和开发?您是否觉得自己总是在反复编写相同的数据访问代码?您是否曾经将数据访问代码包装在 Helper 函数中,以便能够在一行中调用存储过程?如果是,那么 Microsoft® Data Access Application Block for .NET 正是为您设计的。 Data Access Application Block 将访问 Microsoft SQL Server™ 数据库的性能和资源管理方面的最佳经验封装在一起。您可以很方便地在自己的 .NET 应用程序中将其作为构造块使用,从页减少了需要创建、测试和维护的自定义代码的数量。 尤其是,Data Access Application Block 可以帮助您: 调用存储过程或 SQL 文本命令。 指定参数详细信息。 返回 SqlDataReader、DataSet 或 XmlReader 对象。 例如,在引用了 Data Access Application Block 的应用程序中,您可以简单地在一行代码中调用存储过程并生成 DataSet,如下所示: [C#] DataSet ds = SqlHelper.ExecuteDataset( connectionString, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", categoryID)); 注意: Application Block for .NET(用于 .NET 的应用程序块)是基于对成功的 .NET 应用程序进行详细研究而设计的。它以源代码的形式提供,您可以原样使用,也可以针对自己的应用程序进行自定义。该应用程序块并不代表未来 Microsoft ADO.NET 程序库的发展方向。Microsoft ADO.NET 程序库是为在各种使用情况下实现对数据访问行为的精确控制而建立的。将来的 ADO.NET 版本可能会使用不同的模型来实现这个方案 SqlHelper 类提供了一组静态方法,可以用来向 SQL Server 数据库发出许多各种不同类型的命令。 SqlHelperParameterCache 类提供命令参数缓存功能,可以用来提高性能。该类由许多 Execute 方法(尤其是那些只运行存储过程的重写方法)在内部使用。数据访问客户端也可以直接使用它来缓存特定命令的特定参数集。 使用 SqlHelper 类执行命令 SqlHelper 类提供了五种 Shared (Visual Basic) 或 static (C#) 方法,它们是:ExecuteNonQuery、ExecuteDataset、ExecuteReaderExecuteScalar 和 ExecuteXmlReader。实现的每种方法都提供一组一致的重载。这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。类中实现的所有方法都支持以下重载: [C#] Execute* (SqlConnection connection, CommandType commandType, string commandText) Execute* (SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) Execute* (SqlConnection connection, CommandType commandType, string commandText) Execute* (SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) 除这些重载以外,除 ExecuteXmlReader 之外的其他方法还提供了另一种重载:允许将连接信息作为连接字符串而不是连接对象来传递,如下面的方法签名所示: [Visual Basic] Execute* (ByVal connectionString As String, _ ByVal commandType As CommandType, _ ByVal commandText As String) Execute* (ByVal connectionString As String, _ ByVal commandType As CommandType, _ ByVal commandText As String, _ ByVal ParamArray commandParameters() As SqlParameter) Execute* (ByVal connectionString As String, _ ByVal spName As String, _ ByVal ParamArray parameterValues() As Object) [C#] Execute* (string connectionString, CommandType commandType, string commandText) Execute* (string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (string connectionString, string spName, params object[] parameterValues) 注意: ExecuteXmlReader 不支持连接字符串,因为:与 SqlDataReader 对象不同,XmlReader 对象在 XmlReader 关闭时没有提供自动关闭连接的方法。如果客户端传递了连接字符串,那么当客户端完成对 XmlReader 的操作后,将无法关闭与 XmlReader 相关联的连接对象。 通过参考 Data Access Application Block 程序集并导入 Microsoft.ApplicationBlocks.Data 命名空间,您可以轻松编写使用任何一种 SqlHelper 类方法的代码,如下面的代码示例所示: [Visual Basic] Imports Microsoft.ApplicationBlocks.Data [C#] using Microsoft.ApplicationBlocks.Data; 导入命名空间后,您可以调用任何 Execute* 方法,如下面的代码示例所示: [Visual Basic] Dim ds As DataSet = SqlHelper.ExecuteDataset( _ "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;", _ CommandType.Text, "SELECT * FROM Products") [C#] DataSet ds = SqlHelper.ExecuteDataset( "SERVER=DataServer;DATABASE=Northwind;INTEGRATED SECURITY=sspi;", _ CommandType.Text, "SELECT * FROM Products"); 使用 SqlHelperParameterCache 类管理参数 SqlHelperParameterCache 类提供了三种可以用来管理参数的公共共享方法。它们是: CacheParameterSet。用于将 SqlParameters 数组存储到缓存中。 GetCachedParameterSet。用于检索缓存的参数数组的副本。 GetSpParameterSet。一种重载方法,用于检索指定存储过程的相应参数(首先查询一次数据库,然后缓存结果以便将来查询)。 缓存和检索参数 通过使用 CacheParameterSet 方法,可以缓存 SqlParameter 对象数组。此方法通过将连接字符串和命令文本连接起来创建一个键,然后将参数数组存储在 Hashtable 中。 要从缓存中检索参数,请使用 GetCachedParameterSet 方法。此方法将返回一个 SqlParameter 对象数组,这些对象已使用缓存(与传递给该方法的连接字符串和命令文本相对应)中的参数的名称、值、方向和数据类型等进行了初始化。 注意: 用作参数集的键的连接字符串通过简单的字符串比较进行匹配。用于从 GetCachedParameterSet 中检索参数的连接字符串必须与用来通过 CacheParameterSet 来存储这些参数的连接字符串完全相同。语法不同的连接字符串即使语义相同,也不会被认为是匹配的。 以下代码显示了如何使用 SqlHelperParameterCache 类来缓存和检索 Transact-SQL 语句的参数。 [Visual Basic] 初始化连接字符串和命令文本 它们将构成用来存储和检索参数的键 Const CONN_STRING As String = _ "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;" Dim sql As String = _ "SELECT ProductName FROM Products " + _ "WHERE Category=@Cat AND SupplierID = @Sup" 缓存参数 Dim paramsToStore(1) As SqlParameter paramsToStore(0) = New SqlParameter("@Cat", SqlDbType.Int) paramsToStore(1) = New SqlParameter("@Sup", SqlDbType.Int) SqlHelperParameterCache.CacheParameterSet(CONN_STRING, _ sql, _ paramsToStore) 从缓存中检索参数 Dim storedParams(1) As SqlParameter storedParams = SqlHelperParameterCache.GetCachedParameterSet( _ CONN_STRING, sql) storedParams(0).Value = 2 storedParams(1).Value = 3 在命令中使用参数 Dim ds As DataSet ds = SqlHelper.ExecuteDataset(CONN_STRING, _ CommandType.Text, _ sql, storedParams) [C#] // 初始化连接字符串和命令文本 // 它们将构成用来存储和检索参数的键 const string CONN_STRING = "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;"; string spName = "SELECT ProductName FROM Products " + "WHERE Category=@Cat AND SupplierID = @Sup"; // 缓存参数 SqlParameter[] paramsToStore = new SqlParameter[2]; paramsToStore[0] = New SqlParameter("@Cat", SqlDbType.Int); paramsToStore[1] = New SqlParameter("@Sup", SqlDbType.Int); SqlHelperParameterCache.CacheParameterSet(CONN_STRING, sql, paramsToStore); // 从缓存中检索参数 SqlParameter storedParams = new SqlParameter[2]; storedParams = SqlHelperParameterCache.GetCachedParameterSet( CONN_STRING, sql); storedParams(0).Value = 2; storedParams(1).Value = 3; // 在命令中使用参数 DataSet ds; ds = SqlHelper.ExecuteDataset(CONN_STRING, CommandType.StoredProcedure, sql, storedParams); 检索存储过程参数 SqlHelperParameterCache 还提供了针对特定存储过程检索参数数组的方法。一种名为 GetSpParameterSet 的重载方法提供了此功能,它包含两种实现。该方法尝试从缓存中检索特定存储过程的参数。如果这些参数尚未被缓存,则使用 .NET 的 SqlCommandBuilder 类从内部检索,并将它们添加到缓存中,以便用于后续的检索请求。然后,为每个参数指定相应的参数设置,最后将这些参数以数组形式返回给客户端。以下代码显示了如何检索 Northwind 数据库中 SalesByCategory 存储过程的参数。 [Visual Basic] 初始化连接字符串和命令文本 它们将构成用来存储和检索参数的键 Const CONN_STRING As String = _ "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;" Dim spName As String = "SalesByCategory" 检索参数 Dim storedParams(1) As SqlParameter storedParams = SqlHelperParameterCache.GetSpParameterSet( _ CONN_STRING, spName) storedParams(0).Value = "Beverages" storedParams(1).Value = "1997" 在命令中使用参数 Dim ds As DataSet ds = SqlHelper.ExecuteDataset(CONN_STRING, _ CommandType.StoredProcedure, _ spName, storedParams) [C#] // 初始化连接字符串和命令文本 // 它们将构成用来存储和检索参数的键 const string CONN_STRING = "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;"; string spName = "SalesByCategory"; // 检索参数 SqlParameter storedParams = new SqlParameter[2]; storedParams = SqlHelperParameterCache.GetSpParameterSet( CONN_STRING, spName); storedParams[0].Value = "Beverages"; storedParams[1].Value = "1997"; // 在命令中使用参数 DataSet ds; ds = SqlHelper.ExecuteDataset(CONN_STRING, CommandType.StoredProcedure, spName, storedParams); 内部设计 Data Access Application Block 包含了完整的源代码和有关其设计的综合指南。本节介绍有关主要实现的详细信息。 SqlHelper 类实现详细信息 SqlHelper 类用于通过一组静态方法来封装数据访问功能。该类不能被继承或实例化,因此将其声明为包含专用构造函数的不可继承类。 在 SqlHelper 类中实现的每种方法都提供了一组一致的重载。这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。在 SqlHelper 类中实现的方法包括: ExecuteNonQuery。此方法用于执行不返回任何行或值的命令。这些命令通常用于执行数据库更新,但也可用于返回存储过程的输出参数。 ExecuteReader。此方法用于返回 SqlDataReader 对象,该对象包含由某一命令返回的结果集。 ExecuteDataset。此方法返回 DataSet 对象,该对象包含由某一命令返回的结果集。 ExecuteScalar。此方法返回一个值。该值始终是该命令返回的第一行的第一列。 ExecuteXmlReader。此方法返回 FOR XML 查询的 XML 片段。 除了这些公共方法外,SqlHelper 类还包含一些专用函数,用于管理参数和准备要执行的命令。不管客户端调用什么样的方法实现,所有命令都通过 SqlCommand 对象来执行。在 SqlCommand 对象能够被执行之前,所有参数都必须添加到 Parameters 集合中,并且必须正确设置 Connection、CommandType、CommandText 和 Transaction 属性。SqlHelper 类中的专用函数主要用于提供一种一致的方式,以便向 SQL Server 数据库发出命令,而不考虑客户端应用程序调用的重载方法实现。SqlHelper 类中的专用实用程序函数包括: AttachParameters:该函数用于将所有必要的 SqlParameter 对象连接到正在运行的 SqlCommand。 AssignParameterValues:该函数用于为 SqlParameter 对象赋值。 PrepareCommand:该函数用于对命令的属性(如连接、事务环境等)进行初始化ExecuteReader:此专用 ExecuteReader 实现用于通过适当的 CommandBehavior 打开 SqlDataReader 对象,以便最有效地管理与阅读器关联的连接的有效期。 SqlHelperParameterCache 类实现详细信息 参数数组缓存在专用 Hashtable 中。从缓存中检索的参数进行内部复制,这样客户端应用程序能够更改参数值以及进行其他操作,而不会影响缓存的参数数组。专用共享函数 CloneParameters 可以实现此目的。 常见问题 此版本包含哪些新增功能? 与 Data Access Application Block Beta 2.0 版本相比,该 RTM 版本包含以下新增功能和变化: SqlHelper 类方法的事务型重载不再需要 SqlConnection 参数。在此版本中,连接信息从 SqlTransaction 对象中派生,因此不必在方法签名中包含 SqlConnection 对象参数。 现在,GetSpParameterSet 方法使用 ADO.NET CommandBuilder 类的 DeriveParameters 方法来确定存储过程所需要的参数。这比 Beta 2.0 版本中直接通过查询数据库来检索信息的效率更高。 可以使用 XCOPY 部署方法来部署 Data Access Application Block 程序集吗? 可以。Microsoft.ApplicationBlocks.Data.dll 程序集在编译后可以使用 XCOPY 进行部署。 什么时候应该使用 ExecuteDataset 方法,什么时候应该使用 ExecuteReader 方法? 这个问题实际上是什么时候应该返回 DataSet 对象中的多个数据行,什么时候应该使用 DataReader。答案取决于您的应用程序的特定需要,以及您在灵活性和原始性能之间的取舍。DataSet 为您提供数据的灵活的且断开连接的关系视图,而 DataReader 为您提供性能卓越的、只读的、仅向前光标。有关 DataSet 和 DataReader 的全面比较,请参阅 asp">Data Access Architecture Guide(英文)。 如何使用 ExecuteDataset 返回包含多个表的数据集? 通过创建一个可以返回多个行集的存储过程(通过执行多个 SELECT 语句或者对其他存储过程进行嵌套调用),并使用 ExecuteDataset 方法执行该过程,您可以检索包含多个表的数据集。 例如,假设您的数据库包含以下存储过程。 CREATE PROCEDURE GetCategories AS SELECT * FROM Categories GO CREATE PROCEDURE GetProducts AS SELECT * FROM Products 您可以创建一个主存储过程来对这些过程进行嵌套调用,如下面的代码示例所示。 CREATE PROCEDURE GetCategoriesAndProducts AS BEGIN EXEC GetCategories EXEC GetProducts END 使用 ExecuteDataset 方法执行此主存储过程将返回一个 DateSet,其中包含两个表:一个表包含分类数据,另一个表包含产品数据。 注意: ExecuteDataset 方法不提供为返回的表指定自定义名称的方法。第一个表的编号始终为 0,名称为 Table,第二个表的编号为 1,名称为 Table1,依此类推。
.net数据访问类 SQL Helper 介绍 摘要:Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以及向 SQL Server 数据库发出 SQL 文本命令。它返回 SqlDataReader、DataSet 和 XmlReader 对象。您可以在自己的 .NET 应用程序中将其作为构造块来使用,以减少需要创建、测试和维护的自定义代码的数量。您可以下载完整的 C# 和 Visual Basic .NET 源代码以及综合文档。 简介 您是否正在从事 .NET 应用程序数据访问代码的设计和开发?您是否觉得自己总是在反复编写相同的数据访问代码?您是否曾经将数据访问代码包装在 Helper 函数中,以便能够在一行中调用存储过程?如果是,那么 Microsoft? Data Access Application Block for .NET 正是为您设计的。 Data Access Application Block 将访问 Microsoft SQL Server? 数据库的性能和资源管理方面的最佳经验封装在一起。您可以很方便地在自己的 .NET 应用程序中将其作为构造块使用,从页减少了需要创建、测试和维护的自定义代码的数量。 尤其是,Data Access Application Block 可以帮助您: 调用存储过程或 SQL 文本命令。 指定参数详细信息。 返回 SqlDataReader、DataSet 或 XmlReader 对象。 例如,在引用了 Data Access Application Block 的应用程序中,您可以简单地在一行代码中调用存储过程并生成 DataSet,如下所示: [C#] DataSet ds = SqlHelper.ExecuteDataset( connectionString, CommandType.StoredProcedure, "getProductsByCategory", new SqlParameter("@CategoryID", categoryID)); 注意: Application Block for .NET(用于 .NET 的应用程序块)是基于对成功的 .NET 应用程序进行详细研究而设计的。它以源代码的形式提供,您可以原样使用,也可以针对自己的应用程序进行自定义。该应用程序块并不代表未来 Microsoft ADO.NET 程序库的发展方向。Microsoft ADO.NET 程序库是为在各种使用情况下实现对数据访问行为的精确控制而建立的。将来的 ADO.NET 版本可能会使用不同的模型来实现这个方案 SqlHelper 类提供了一组静态方法,可以用来向 SQL Server 数据库发出许多各种不同类型的命令。 SqlHelperParameterCache 类提供命令参数缓存功能,可以用来提高性能。该类由许多 Execute 方法(尤其是那些只运行存储过程的重写方法)在内部使用。数据访问客户端也可以直接使用它来缓存特定命令的特定参数集。 使用 SqlHelper 类执行命令 SqlHelper 类提供了五种 Shared (Visual Basic) 或 static (C#) 方法,它们是:ExecuteNonQuery、ExecuteDataset、ExecuteReaderExecuteScalar 和 ExecuteXmlReader。实现的每种方法都提供一组一致的重载。这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。类中实现的所有方法都支持以下重载: [C#] Execute* (SqlConnection connection, CommandType commandType, string commandText) Execute* (SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) Execute* (SqlConnection connection, CommandType commandType, string commandText) Execute* (SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) 除这些重载以外,除 ExecuteXmlReader 之外的其他方法还提供了另一种重载:允许将连接信息作为连接字符串而不是连接对象来传递,如下面的方法签名所示: [Visual Basic] Execute* (ByVal connectionString As String, _ ByVal commandType As CommandType, _ ByVal commandText As String) Execute* (ByVal connectionString As String, _ ByVal commandType As CommandType, _ ByVal commandText As String, _ ByVal ParamArray commandParameters() As SqlParameter) Execute* (ByVal connectionString As String, _ ByVal spName As String, _ ByVal ParamArray parameterValues() As Object) [C#] Execute* (string connectionString, CommandType commandType, string commandText) Execute* (string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (string connectionString, string spName, params object[] parameterValues) 注意: ExecuteXmlReader 不支持连接字符串,因为:与 SqlDataReader 对象不同,XmlReader 对象在 XmlReader 关闭时没有提供自动关闭连接的方法。如果客户端传递了连接字符串,那么当客户端完成对 XmlReader 的操作后,将无法关闭与 XmlReader 相关联的连接对象。 通过参考 Data Access Application Block 程序集并导入 Microsoft.ApplicationBlocks.Data 命名空间,您可以轻松编写使用任何一种 SqlHelper 类方法的代码,如下面的代码示例所示: [Visual Basic] Imports Microsoft.ApplicationBlocks.Data [C#] using Microsoft.ApplicationBlocks.Data; 导入命名空间后,您可以调用任何 Execute* 方法,如下面的代码示例所示: [Visual Basic] Dim ds As DataSet = SqlHelper.ExecuteDataset( _ "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;", _ CommandType.Text, "SELECT * FROM Products") [C#] DataSet ds = SqlHelper.ExecuteDataset( "SERVER=DataServer;DATABASE=Northwind;INTEGRATED SECURITY=sspi;", _ CommandType.Text, "SELECT * FROM Products"); 使用 SqlHelperParameterCache 类管理参数 SqlHelperParameterCache 类提供了三种可以用来管理参数的公共共享方法。它们是: CacheParameterSet。用于将 SqlParameters 数组存储到缓存中。 GetCachedParameterSet。用于检索缓存的参数数组的副本。 GetSpParameterSet。一种重载方法,用于检索指定存储过程的相应参数(首先查询一次数据库,然后缓存结果以便将来查询)。 缓存和检索参数 通过使用 CacheParameterSet 方法,可以缓存 SqlParameter 对象数组。此方法通过将连接字符串和命令文本连接起来创建一个键,然后将参数数组存储在 Hashtable 中。 要从缓存中检索参数,请使用 GetCachedParameterSet 方法。此方法将返回一个 SqlParameter 对象数组,这些对象已使用缓存(与传递给该方法的连接字符串和命令文本相对应)中的参数的名称、值、方向和数据类型等进行了初始化。 注意: 用作参数集的键的连接字符串通过简单的字符串比较进行匹配。用于从 GetCachedParameterSet 中检索参数的连接字符串必须与用来通过 CacheParameterSet 来存储这些参数的连接字符串完全相同。语法不同的连接字符串即使语义相同,也不会被认为是匹配的。 以下代码显示了如何使用 SqlHelperParameterCache 类来缓存和检索 Transact-SQL 语句的参数。 [Visual Basic] 初始化连接字符串和命令文本 它们将构成用来存储和检索参数的键 Const CONN_STRING As String = _ "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;" Dim sql As String = _ "SELECT ProductName FROM Products " + _ "WHERE Category=@Cat AND SupplierID = @Sup" 缓存参数 Dim paramsToStore(1) As SqlParameter paramsToStore(0) = New SqlParameter("@Cat", SqlDbType.Int) paramsToStore(1) = New SqlParameter("@Sup", SqlDbType.Int) SqlHelperParameterCache.CacheParameterSet(CONN_STRING, _ sql, _ paramsToStore) 从缓存中检索参数 Dim storedParams(1) As SqlParameter storedParams = SqlHelperParameterCache.GetCachedParameterSet( _ CONN_STRING, sql) storedParams(0).Value = 2 storedParams(1).Value = 3 在命令中使用参数 Dim ds As DataSet ds = SqlHelper.ExecuteDataset(CONN_STRING, _ CommandType.Text, _ sql, storedParams) [C#] // 初始化连接字符串和命令文本 // 它们将构成用来存储和检索参数的键 const string CONN_STRING = "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;"; string spName = "SELECT ProductName FROM Products " + "WHERE Category=@Cat AND SupplierID = @Sup"; // 缓存参数 SqlParameter[] paramsToStore = new SqlParameter[2]; paramsToStore[0] = New SqlParameter("@Cat", SqlDbType.Int); paramsToStore[1] = New SqlParameter("@Sup", SqlDbType.Int); SqlHelperParameterCache.CacheParameterSet(CONN_STRING, sql, paramsToStore); // 从缓存中检索参数 SqlParameter storedParams = new SqlParameter[2]; storedParams = SqlHelperParameterCache.GetCachedParameterSet( CONN_STRING, sql); storedParams(0).Value = 2; storedParams(1).Value = 3; // 在命令中使用参数 DataSet ds; ds = SqlHelper.ExecuteDataset(CONN_STRING, CommandType.StoredProcedure, sql, storedParams); 检索存储过程参数 SqlHelperParameterCache 还提供了针对特定存储过程检索参数数组的方法。一种名为 GetSpParameterSet 的重载方法提供了此功能,它包含两种实现。该方法尝试从缓存中检索特定存储过程的参数。如果这些参数尚未被缓存,则使用 .NET 的 SqlCommandBuilder 类从内部检索,并将它们添加到缓存中,以便用于后续的检索请求。然后,为每个参数指定相应的参数设置,最后将这些参数以数组形式返回给客户端。以下代码显示了如何检索 Northwind 数据库中 SalesByCategory 存储过程的参数。 [Visual Basic] 初始化连接字符串和命令文本 它们将构成用来存储和检索参数的键 Const CONN_STRING As String = _ "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;" Dim spName As String = "SalesByCategory" 检索参数 Dim storedParams(1) As SqlParameter storedParams = SqlHelperParameterCache.GetSpParameterSet( _ CONN_STRING, spName) storedParams(0).Value = "Beverages" storedParams(1).Value = "1997" 在命令中使用参数 Dim ds As DataSet ds = SqlHelper.ExecuteDataset(CONN_STRING, _ CommandType.StoredProcedure, _ spName, storedParams) [C#] // 初始化连接字符串和命令文本 // 它们将构成用来存储和检索参数的键 const string CONN_STRING = "SERVER=(local); DATABASE=Northwind; INTEGRATED SECURITY=True;"; string spName = "SalesByCategory"; // 检索参数 SqlParameter storedParams = new SqlParameter[2]; storedParams = SqlHelperParameterCache.GetSpParameterSet( CONN_STRING, spName); storedParams[0].Value = "Beverages"; storedParams[1].Value = "1997"; // 在命令中使用参数 DataSet ds; ds = SqlHelper.ExecuteDataset(CONN_STRING, CommandType.StoredProcedure, spName, storedParams); 内部设计 Data Access Application Block 包含了完整的源代码和有关其设计的综合指南。本节介绍有关主要实现的详细信息。 SqlHelper 类实现详细信息 SqlHelper 类用于通过一组静态方法来封装数据访问功能。该类不能被继承或实例化,因此将其声明为包含专用构造函数的不可继承类。 在 SqlHelper 类中实现的每种方法都提供了一组一致的重载。这提供了一种很好的使用 SqlHelper 类来执行命令的模式,同时为开发人员选择访问数据的方式提供了必要的灵活性。每种方法的重载都支持不同的方法参数,因此开发人员可以确定传递连接、事务和参数信息的方式。在 SqlHelper 类中实现的方法包括: ExecuteNonQuery。此方法用于执行不返回任何行或值的命令。这些命令通常用于执行数据库更新,但也可用于返回存储过程的输出参数。 ExecuteReader。此方法用于返回 SqlDataReader 对象,该对象包含由某一命令返回的结果集。 ExecuteDataset。此方法返回 DataSet 对象,该对象包含由某一命令返回的结果集。 ExecuteScalar。此方法返回一个值。该值始终是该命令返回的第一行的第一列。 ExecuteXmlReader。此方法返回 FOR XML 查询的 XML 片段。 除了这些公共方法外,SqlHelper 类还包含一些专用函数,用于管理参数和准备要执行的命令。不管客户端调用什么样的方法实现,所有命令都通过 SqlCommand 对象来执行。在 SqlCommand 对象能够被执行之前,所有参数都必须添加到 Parameters 集合中,并且必须正确设置 Connection、CommandType、CommandText 和 Transaction 属性。SqlHelper 类中的专用函数主要用于提供一种一致的方式,以便向 SQL Server 数据库发出命令,而不考虑客户端应用程序调用的重载方法实现。SqlHelper 类中的专用实用程序函数包括: AttachParameters:该函数用于将所有必要的 SqlParameter 对象连接到正在运行的 SqlCommand。 AssignParameterValues:该函数用于为 SqlParameter 对象赋值。 PrepareCommand:该函数用于对命令的属性(如连接、事务环境等)进行初始化ExecuteReader:此专用 ExecuteReader 实现用于通过适当的 CommandBehavior 打开 SqlDataReader 对象,以便最有效地管理与阅读器关联的连接的有效期。 SqlHelperParameterCache 类实现详细信息 参数数组缓存在专用 Hashtable 中。从缓存中检索的参数进行内部复制,这样客户端应用程序能够更改参数值以及进行其他操作,而不会影响缓存的参数数组。专用共享函数 CloneParameters 可以实现此目的。 常见问题 此版本包含哪些新增功能? 与 Data Access Application Block Beta 2.0 版本相比,该 RTM 版本包含以下新增功能和变化: SqlHelper 类方法的事务型重载不再需要 SqlConnection 参数。在此版本中,连接信息从 SqlTransaction 对象中派生,因此不必在方法签名中包含 SqlConnection 对象参数。 现在,GetSpParameterSet 方法使用 ADO.NET CommandBuilder 类的 DeriveParameters 方法来确定存储过程所需要的参数。这比 Beta 2.0 版本中直接通过查询数据库来检索信息的效率更高。 可以使用 XCOPY 部署方法来部署 Data Access Application Block 程序集吗? 可以。Microsoft.ApplicationBlocks.Data.dll 程序集在编译后可以使用 XCOPY 进行部署。 什么时候应该使用 ExecuteDataset 方法,什么时候应该使用 ExecuteReader 方法? 这个问题实际上是什么时候应该返回 DataSet 对象中的多个数据行,什么时候应该使用 DataReader。答案取决于您的应用程序的特定需要,以及您在灵活性和原始性能之间的取舍。DataSet 为您提供数据的灵活的且断开连接的关系视图,而 DataReader 为您提供性能卓越的、只读的、仅向前光标。有关 DataSet 和 DataReader 的全面比较,请参阅 asp">Data Access Architecture Guide(英文)。 如何使用 ExecuteDataset 返回包含多个表的数据集? 通过创建一个可以返回多个行集的存储过程(通过执行多个 SELECT 语句或者对其他存储过程进行嵌套调用),并使用 ExecuteDataset 方法执行该过程,您可以检索包含多个表的数据集。 例如,假设您的数据库包含以下存储过程。 CREATE PROCEDURE GetCategories AS SELECT * FROM Categories GO CREATE PROCEDURE GetProducts AS SELECT * FROM Products 您可以创建一个主存储过程来对这些过程进行嵌套调用,如下面的代码示例所示。 CREATE PROCEDURE GetCategoriesAndProducts AS BEGIN EXEC GetCategories EXEC GetProducts END 使用 ExecuteDataset 方法执行此主存储过程将返回一个 DateSet,其中包含两个表:一个表包含分类数据,另一个表包含产品数据。 注意: ExecuteDataset 方法不提供为返回的表指定自定义名称的方法。第一个表的编号始终为 0,名称为 Table,第二个表的编号为 1,名称为 Table1,依此类推。
1、SqlConnection类   构造函数:SqlConnection(connstr);   属性:  Database//获取当前数据库或连接打开后要使用的数据库的名称        Connectionstring//获取或设置用于打开 SQL Server 数据库的字符串   方法:  Open()        Close()        Dispose()//释放所有资源         2、SqlCommand类     构造函数:SqlCommand(string,conn) 属性:  CommandType//获取或设置一个值,该值指示如何解释 CommandText 属         CommandText //获取或设置要对数据源执行SQL 语句         Connection//获取或设置 SqlCommand 的此实例使用的 SqlConnection         Parameters//获取 SqlParameterCollection。参数集合 方法:  ExecuteNonQuery(): 返回受影响函数,如增、删、改操作;         ExecuteScalar():执行查询,返回首行首列的结果;         ExecuteReader():返回一个数据流(SqlDataReader对象)。   实例1: SqlCommand cmd = new SqlCommand () cmd.connection =conn; cmd.CommandType = CommandType.Text; cmd.CommandText ="select *from produce=@ID"; cmd.Parameters.Add("@ID",SqlDBType.NVarChar,10,ID).values=1; cmd.ExecuteScalar();   实例2:   SqlCommand cmd = new SqlCommand (“select *from test”,conn);   cmd.ExecuteScalar(); 注意: ExecuteNonQuery()方法主要用户更新数据,通常它使用Update,Insert,Delete语句来操作数据库,其方法返回值意义:对于 Update,Insert,Delete 语句 执行成功是返回值为该命令所影响的行数,如果影响的行数为0时返回的值为0,如果数据操作回滚得话返回值为-1,对于这种更新操作 用我们平时所用的是否大于0的判断操作应该没有问题而且比较好,但是对于其他的操作如对数据库结构的操作,如果操作成功时返回的却是-1,这种情况跟我们平时的思维方式有点差距所以应该好好的注意了, 3、SqlDataReader类   是由ExecuteReader()返回一个数据流(SqlDataReader对象)没有构造方法   属性:Connection//获取与 SqlDataReader 关联的 SqlConnection。      FieldCount//获取当前行中的列数。      HasRows//获取一个值,该值指示 SqlDataReader 是否包含一行或多行      RecordsAffect//获取执行 Transact-SQL 语句所更改、插入或删除的行数   方法:Read();//使 SqlDataReader 前进到下一条记录      GetType();//获取当前实例的 Type      NextResult();//当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果 4、SqlDataApater类   构造方法:SqlDataApater()        SqlDataAdapter(SqlCommand)//初始化 SqlDataAdapter 类的新实例,用指定的 SqlCommand 作为 SelectCommand属性。        SqlDataApater(string,conn)//使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的一个新实例   属性:  DeleteCommand        SelectCommand        InsertCommand        UpdataCommand              方法:  Fill(DataSet)//在 DataSet 中添加或刷新行        Fill(DataTable)//在 DataSet 的指定范围中添加或刷新行,以与使用 DataTable 名称的数据源中的行匹配。 (继承自DbDataAdapter。        Update(DataRow[])//通过为 DataSet 中的指定数组中的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库中的值        Update(DataSet)//通过为指定的 DataTable 中的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库中的值        Update(DataTable) 实例1   // 隐藏了SqlCommand对象的定义,同时隐藏了SqlCommand对象与SqlDataAdapter对象的绑定 SqlDataAdapter myda= new SqlDataAdapter("select * from test",conn);   实例2   SqlCommand mySqlCommand = new SqlCommand();// 创建SqlCommand   mySqlCommand.CommandType = CommandType.Text;   mySqlCommand.CommandText = "select * from product";   mySqlCommand.Connection = sqlCnt;   SqlDataAdapter myDataAdapter = new SqlDataAdapter();   // 创建SqlDataAdapter   myDataAdapter.SelectCommand = mySqlCommand; // 为SqlDataAdapter对象绑定所要执行的SqlCommand对象 5、DataSet类     命名空间:System.Data.DataSet。   数据集,本地微型数据库,可以存储多张表。  //使用DataSet第一步就是将SqlDataAdapter返回的数据集(表)填充到Dataset对象中:   SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from product", sqlCnt); DataSet myDataSet = new DataSet(); // 创建DataSet myDataAdapter.Fill(myDataSet, "product"); // 将返回的数据集作为“表”填入DataSet中,表名可以与数据库真实的表名不同,并不影响后续的增、删、改等操作 //访问dataset中的数据 DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { foreach (DataColumn myColumn in myTable.Columns) { Console.WriteLine(myRow[myColumn]); //遍历表中的每个单元格 } } // 修改DataSet DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { myRow["name"] = myRow["name"] + "商品"; } // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 添加一行 DataRow myRow = myTable.NewRow(); myRow["name"] = "捷安特"; myRow["price"] = 13.2; //myRow["id"] = 100; id若为“自动增长”,此处可以不设置,即便设置也无效 myTable.Rows.Add(myRow); // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 删除第一行 DataTable myTable = myDataSet.Tables["product"]; myTable.Rows[0].Delete(); SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product");
《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用的属性、方法、类和各种技术,每一个知识点都配有具体的示例,便于读者理解。 《C#全能速查宝典》所讲的知识点按照功能和字母进行排序,读者既可以按照功能顺序查找,又可以按照字母顺序学习。 《C#全能速查宝典》不仅适合C#程序设计初学者,也可作为中、高级程序开发人员的参考手册。 ============================================================ 图书目录 第1章 C#语言基础 1 1.1 常用概念、关键字及基础类 1 1.1.1 abstract关键字——抽象 1 1.1.2 as操作符——引用类型转换 3 1.1.3 base关键字——从派生类中访问基类的成员 3 1.1.4 变量——存储特定类型的数据 4 1.1.5 Console类——控制台中的输入流、输出流和错误流 6 1.1.6 Convert类——类型转换 8 1.1.7 常量——值不改变的量 9 1.1.8 Dispose方法——释放资源 10 1.1.9 迭代器——相同类型的值的有序序列的一段代码 10 1.1.10 泛型——处理算法和数据结构 11 1.1.11 分部类——将一个类分成几部分 12 1.1.12 is操作符——检查变量是否为指定的类型 14 1.1.13 lock关键字——锁定 15 1.1.14 namespace关键字——定义命名空间 15 1.1.15 new运算符——创建一个新的类型实例 16 1.1.16 Object类型——所有类型的基类 17 1.1.17 OOP技术——面向对象编程技术 18 1.1.18 ReadLine方法——从当前流中读取一行字符 20 1.1.19 typeof运算符——获得系统原型对象的类型 21 1.1.20 using关键字——引入命名空间 22 1.1.21 WriteLine方法——写入流 23 1.2 数学方法类——Math 25 1.2.1 Abs方法——返回指定数字的绝对值 25 1.2.2 Acos方法——返回余弦值为指定数字的角度 26 1.2.3 Asin方法——返回正弦值为指定数字的角度 26 1.2.4 Atan方法——返回正切值为指定数字的角度 27 1.2.5 Pow方法——返回指定数字的指定次幂 27 1.2.6 Round方法——将小数值舍入到指定的精度 28 1.3 流程控制语句 29 1.3.1 break语句——跳出循环 29 1.3.2 case语句——比较表达式以确定结果 30 1.3.3 continue语句——继续执行下一个循环 31 1.3.4 do…while语句——循环语句 31 1.3.5 for语句——循环语句 32 1.3.6 foreach语句——枚举一个集合的元素 33 1.3.7 goto语句——跳转到标签 34 1.3.8 if…else语句——条件判断语句 36 1.3.9 return语句——返回 38 1.3.10 switch case语句——条件判断语句 39 1.3.11 throw语句——显式引发异常 40 1.3.12 try…catch…finally语句——捕捉异常 42 1.3.13 while语句——循环语句 43 1.4 字符串处理 44 1.4.1 AddDays方法——添加天数 44 1.4.2 AddString方法——添加文本字符串 45 1.4.3 Compare方法——比较两个字符串 46 1.4.4 CompareTo方法——比较两个字符串对象 47 1.4.5 DATEADD函数——在指定日期上加一段时间 48 1.4.6 DateDiff方法——获取日期时间的间隔数 48 1.4.7 DateTime结构——表示时间上的一刻 50 1.4.8 DAY函数——返回日期部分的整数 51 1.4.9 DayOfWeek属性——获取星期几 52 1.4.10 Equals方法——比较两个字符串对象 53 1.4.11 First函数——返回查询结果的第一个记录 55 1.4.12 FirstDayOfWeek属性——获取或设置一周中的第一天 56 1.4.13 Format方法——格式化字符串 56 1.4.14 GETDATE函数——返回当前系统日期和时间 58 1.4.15 GetDayOfMonth方法——返回几号 59 1.4.16 GetDayOfWeek方法——返回星期几 59 1.4.17 GetDayOfYear方法——返回第几天 60 1.4.18 GetDaysInMonth方法——返回指定月份中的天数 60 1.4.19 GetDaysInYear方法——返回指定年份中的天数 61 1.4.20 GetMonth方法——返回指定日期中的月份 61 1.4.21 GetMonthsInYear方法——返回指定年份的月数 62 1.4.22 GetText方法——检索文本数据 63 1.4.23 GetYear方法——返回指定日期中的年份 64 1.4.24 IndexOf方法——确定指定字符在字符串中的索引 65 1.4.25 IsLeapYear方法——判断年份是否为闰年 67 1.4.26 IsMatch方法——搜索正则表达式匹配项 67 1.4.27 IsUpper方法——判断是否大写 68 1.4.28 Join方法——串联字符串 69 1.4.29 LastIndexOf方法——确定字符在字符串中最后索引 70 1.4.30 Matches方法——检查字符串是否有重复的词出现 71 1.4.31 MONTH函数——返回指定日期中月部分的整数 73 1.4.32 PadLeft方法——在左边用空格填充 73 1.4.33 PadRight方法——在右边用空格填充 74 1.4.34 Random类——伪随机数生成器 75 1.4.35 Regex类——正则表达式 76 1.4.36 Split方法——分割字符串 78 1.4.37 String类——字符串 79 1.4.38 StringBuilder类——可变字符串 82 1.4.39 Substring方法——截取字符串 83 1.4.40 TimeSpan对象——表示时间间隔或持续时间 84 1.4.41 ToInt32方法——转换为32位有符号整数 85 1.4.42 ToLongDateString 方法——转换为长日期字符串 86 1.4.43 ToLongTimeString 方法——转换为长时间字符串 87 1.4.44 ToLower方法——转换为小写 87 1.4.45 ToShortDateString方法——转换为短日期字符串 88 1.4.46 ToShortTimeString方法——转换为短时间字符串 88 1.4.47 ToString方法——转换为字符串 89 1.4.48 ToUpper方法——转换为大写 90 1.4.49 Trim方法——移除所有空白字符 91 1.4.50 TrimEnd方法——从尾部移除匹配项 92 1.4.51 TrimStart方法——从开始移除匹配项 92 1.4.52 YEAR函数——返回指定日期的年份的整数 93 1.5 数组与集合 93 1.5.1 Add方法——添加项 93 1.5.2 ArrayList类——集合 95 1.5.3 AsEnumerable方法——转换为IEnumerable类型 97 1.5.4 Clear方法——清空内容 98 1.5.5 Contains方法——确定是否包含某项 99 1.5.6 ContainsKey方法——确定哈希表是否包含特定键 100 1.5.7 ContainsText方法——确定剪贴板中是否存在数据 101 1.5.8 ContainsValue方法——确定哈希表是否包含特定值 101 1.5.9 Count属性——获取数目 102 1.5.10 GetEnumerator方法——循环访问对象 103 1.5.11 GetEnvironmentVariables方法——检索环境变量 104 1.5.12 Hashtable类——哈希表 106 1.5.13 Insert方法——插入项 110 1.5.14 Item属性——获取或设置指定索引处的元素 111 1.5.15 Length属性——获取长度 112 1.5.16 Next方法——返回一个指定范围内的随机数 113 1.5.17 Queue类——队列 115 1.5.18 Remove方法——移除指定项 116 1.5.19 RemoveAt方法——移除指定索引处的项 118 1.5.20 Replace方法——替换文件或字符串 119 1.5.21 Reverse方法——反转数组元素 120 1.5.22 Sort方法——数组排序 121 1.5.23 Stack类——堆栈 123 第2章 Windows窗体及常用控件 126 2.1 Form窗体 126 2.1.1 AcceptButton属性——设置接受按钮 126 2.1.2 Activate事件——当激活窗体时发生 126 2.1.3 Appllication类——提供管理应用程序的静态方法 126 2.1.4 CancelButton属性——设置取消按钮 128 2.1.5 Computer类——提供操作计算机组件的属性 129 2.1.6 ComputerInfo类——获取计算机信息 130 2.1.7 Control类——定义控件基类 131 2.1.8 Environment类——提供当前环境和平台的信息 134 2.1.9 Form窗体——可视化界面 136 2.1.10 FormClosed事件——关闭窗体后事件 139 2.1.11 FormClosing事件——关闭窗体前事件 139 2.1.12 Icon属性——设置图标 139 2.1.13 IsMdiContainer属性——设置父窗体 140 2.1.14 LayoutMdi方法——排列子窗体 141 2.1.15 Load事件——窗体加载事件 141 2.1.16 MaximizeBox属性——是否显示最大化按钮 142 2.1.17 Maximum属性——设置数字显示框的最大值 142 2.1.18 MDI窗体——多文档界面 143 2.1.19 MdiChildren属性——获取子窗体的数组 146 2.1.20 MdiParent属性——设置父窗体 147 2.1.21 MinimizeBox属性——是否显示最小化按钮 147 2.1.22 Minimum属性——数字显示框的最小值 148 2.1.23 Opacity属性——设置窗体的透明度级别 148 2.1.24 Owner属性——设置窗体所有者 149 2.1.25 StartPosition属性——设置窗体起始位置 150 2.1.26 StartupPath 属性——获取可执行文件路径 150 2.1.27 TopMost属性——窗体是否应显示为最顶层窗体 151 2.1.28 WindowState属性——窗体的窗口状态 151 2.2 文本类控件 152 2.2.1 AllowEdit属性——是否可以编辑列表项 152 2.2.2 AppendText方法——追加文本 152 2.2.3 BeginEdit方法——将单元格置于编辑模式下 153 2.2.4 Button控件——按钮控件 153 2.2.5 CancelEdit属性——取消更改 155 2.2.6 CanPaste方法——是否可以粘贴数据 155 2.2.7 CanRedo属性——是否有可以重新应用的操作 156 2.2.8 CanSelect属性——是否可以选中控件 157 2.2.9 CanUndo属性——能否撤销上一个操作 157 2.2.10 Cut方法——将选定内容移动到“剪贴板”中 158 2.2.11 Find方法——搜索指定的项目 158 2.2.12 FindString方法——搜索文本 160 2.2.13 Label控件——标签控件 161 2.2.14 LabelEdit属性——允许用户编辑控件数据 163 2.2.15 LinkLabel控件——以超链接形式显示文本 164 2.2.16 MaskedTextBox控件——使用掩码区分用户输入 166 2.2.17 Multiline属性——是否为多行输入数据 169 2.2.18 PasswordChar属性——取代用户输入而显示的字符 170 2.2.19 Redo方法——重新应用控件中上次撤销的操作 171 2.2.20 RichTextBox控件——有格式文本控件 171 2.2.21 Select方法——激活控件 173 2.2.22 SelectAll方法——选定所有文本 176 2.2.23 Selected属性——是否选定 176 2.2.24 SelectedCells属性——用户选定的单元格集合 177 2.2.25 SelectedColumns属性——用户选定的列集合 178 2.2.26 SelectedRows属性——用户选定的行集合 179 2.2.27 SelectionBackColor属性——文本在选中时的颜色 180 2.2.28 SelectionColor属性——插入点的文本颜色 180 2.2.29 SelectionEnd属性——设置选定日期范围的结束日期 181 2.2.30 SelectionFont属性——选定文本或插入点的字体 182 2.2.31 SelectionIndent属性——所选内容开始行的缩进距离 183 2.2.32 SelectionLength属性——控件中选定的字符数 184 2.2.33 SelectionRange 属性——设置选定的日期范围 185 2.2.34 SelectionStart属性——选择的起始位置的字符索引 185 2.2.35 TextBox控件——输入或显示文本 186 2.2.36 TextChanged事件——Text属性值更改时发生 187 2.3 选择类控件 188 2.3.1 CheckBox控件——复选框控件 188 2.3.2 CheckBoxes属性——是否显示复选框 190 2.3.3 Checked属性——复选框是否处于选中状态 190 2.3.4 CheckedChanged事件——Checked属性更改时发生 191 2.3.5 CheckedListBox控件——复选框列表控件 191 2.3.6 CheckState属性——设置CheckBox控件的状态 193 2.3.7 ComboBox控件——下拉组合框控件 194 2.3.8 DomainUpDown控件——上下选择控件 195 2.3.9 DropDownStyle属性——指定组合框样式的值 197 2.3.10 GetItemCheckState方法——当前项的复选状态的值 198 2.3.11 GetItemText方法——指定项的文本表示形式 199 2.3.12 Index属性——从零开始的索引 200 2.3.13 Items属性——数组列表对象中的项的集合 200 2.3.14 ListBox控件——列表控件 201 2.3.15 ListView控件——显示带图标的项列表 205 2.3.16 NumericUpDown控件——数值选择控件 208 2.3.17 RadioButton控件——单选按钮 210 2.3.18 SelectedIndex属性——获取选择项的索引 212 2.3.19 SelectedIndices属性——表示当前选中的项 213 2.3.20 SelectedItem属性——当前选中的项 214 2.3.21 SelectedItems属性——选定项的集合 215 2.3.22 SelectedText属性——选定文本 216 2.4 容器类控件 217 2.4.1 FlatStyle属性——设置控件的平面样式外观 217 2.4.2 FlowDirection属性——指示FlowLayoutPanel控件的流向 217 2.4.3 FlowLayoutPanel控件——水平或垂直排列内容 218 2.4.4 GroupBox控件——分组控件 219 2.4.5 Panel控件——容器控件 220 2.4.6 TabControl控件——选项卡控件 222 2.4.7 TabIndex属性——控件的Tab键顺序 224 2.4.8 TabPages属性——选项卡页的集合 224 第3章 Windows高级控件 226 3.1 日期时间类控件 226 3.1.1 CalendarFont属性——日历的字体样式 226 3.1.2 CalendarForeColor属性——日历的前景色 226 3.1.3 DateTimePicker控件——日期和日历的组合 226 3.1.4 MaxDate属性——最大日期和时间 228 3.1.5 MinDate属性——最小日期和时间 228 3.1.6 MonthCalendar控件——以网格形式显示日历 229 3.1.7 SetDate方法——将日期设置为当前选定的日期 231 3.1.8 ShowToday属性——是否显示当前日期 232 3.2 对话框、菜单、工具栏及状态栏控件 232 3.2.1 ColorDialog控件——颜色对话框 232 3.2.2 ContextMenuStrip控件——右键快捷菜单 233 3.2.3 ExpandAll方法——展开所有树节点 233 3.2.4 Filter属性——设置筛选器字符串 234 3.2.5 FolderBrowserDialog控件——浏览文件夹对话框 234 3.2.6 Font属性——设置字体 235 3.2.7 FontDialog控件——字体对话框 235 3.2.8 InitialDirectory属性——文件对话框显示的初始目录 237 3.2.9 MenuStrip控件——菜单控件 238 3.2.10 Nodes属性——树节点集合 241 3.2.11 OpenFileDialog控件——打开文件对话框 241 3.2.12 RestoreDirectory属性——是否还原当前目录 244 3.2.13 RootFolder属性——设置浏览的根文件夹 245 3.2.14 SaveFileDialog组件——保存文件对话框 246 3.2.15 SelectedNode属性——获取选定的树节点 248 3.2.16 SelectedPath属性——用户选定的路径 249 3.2.17 ShowDialog方法——打开模式对话框 249 3.2.18 ToolStrip控件——工具栏控件 251 3.2.19 TreeNode类——树节点 252 3.2.20 TreeView控件——树控件 254 3.3 数据绑定类控件 256 3.3.1 BindingNavigator控件——导航和操作数据 256 3.3.2 Cell对象——表示Word文档中的单元格 258 3.3.3 CellClick事件——单元格的任何部分被单击时发生 259 3.3.4 CellEnter事件——控件接收到输入焦点时发生 260 3.3.5 CellMouseClick事件——鼠标单击单元格时发生 261 3.3.6 CellLeave事件——单元格失去输入焦点时发生 261 3.3.7 Cells属性——Bookmark控件中的表单元格 261 3.3.8 ColumnCount属性——DataGridView控件显示的列数 262 3.3.9 Columns属性——控件中所有列的集合 262 3.3.10 ColumnWidth属性——ListBox中列的宽度 263 3.3.11 CurrentCell属性——设置当前处于活动状态的单元格 263 3.3.12 CurrentRow属性——包含当前单元格的行 263 3.3.13 DataGridView控件——数据控件 264 3.3.14 FullRowSelect属性——是否选择其所有子项 268 3.3.15 GetCellCount方法——获取满足筛选器的单元格数目 269 3.3.16 GetColumn方法——指定子控件的列位置 270 3.3.17 NewRow方法——添加一条新记录 270 3.3.18 RowCount方法——DataGridView中显示的行数 271 3.3.19 Rows属性——DataGridView控件中的所有行 272 3.4 打印类控件 273 3.4.1 CrystalReportViewer控件——水晶报表查看控件 273 3.4.2 Document属性——设置要预览的文档 280 3.4.3 PageSetupDialog组件——配置页面的对话框 281 3.4.4 Print方法——打印当前页面 283 3.4.5 PrintDialog组件——打印对话框 283 3.4.6 PrintDocument组件——设置打印的文档 286 3.4.7 PrinterSettings属性——打印机设置 291 3.4.8 PrintPage事件——当需要为当前页打印的输出时发生 292 3.4.9 PrintPreviewControl组件——按文档打印时的外观显示Print Document组件 292 3.4.10 PrintPreviewDialog组件——显示PrintDocument组件在打印时的外观 295 3.4.11 PrinterSettings类——用来指定有关文档打印方式的信息 297 3.4.12 Zoom属性——指示页面的显示大小 300 3.5 其他常用组件 300 3.5.1 BackgroundWorker组件——在主线程的另一线程上异步执行耗时的操作 300 3.5.2 ErrorProvider控件——检查并显示错误信息 302 3.5.3 EventLog组件——连接本地和远程计算机的事件日志 303 3.5.4 HelpProvider组件——将帮助文件与Windows应用程序相关联 306 3.5.5 HScrollBar控件——一个标准Windows水平滚动条 309 3.5.6 Image属性——显示在控件上的图像 311 3.5.7 ImageAlign属性——在控件中显示的图像的对齐方式 312 3.5.8 ImageFormat类——指定图像的格式 312 3.5.9 ImageList组件——用于存储图像 314 3.5.10 ImageList属性——在控件中显示的图像的ImageList 316 3.5.11 Interval属性——设置Timer控件执行的间隔 317 3.5.12 NotifyIcon控件——设置程序的系统托盘图标 317 3.5.13 PerformStep方法——按照Step属性的数量增加进度栏的当前位置 319 3.5.14 PictrueBox控件——用于显示指定的图像 320 3.5.15 Play方法——播放.wav文件 323 3.5.16 ProgressBar控件——进度条 323 3.5.17 SetError方法——设置错误信息 326 3.5.18 SetShowHelp方法——是否显示帮助信息 327 3.5.19 SetToolTip方法——设置提示文本 328 3.5.20 Step属性——增加进度条的当前位置时所根据的数量 328 3.5.21 Stop方法——停止加载网页 329 3.5.22 Tick事件——计时器处于启用状态时发生 330 3.5.23 Timer组件——定期引发事件的组件 330 3.5.24 ToolTip控件——显示提示信息 332 3.5.25 ToolTipIcon属性——提示文本旁显示的图标类型 333 3.5.26 ToolTipText属性——ToolTip显示的文本 334 3.5.27 ToolTipTitle属性——工具提示窗口的标题 334 3.5.28 TrackBar控件——标准的Windows跟踪条 335 3.5.29 Url属性——引用服务说明的URL 337 3.5.30 VscrollBar控件——标准的Windows垂直滚动条 337 3.5.31 WebBrowser控件——在窗体中显示网页 339 3.5.32 Windows Media Player控件——播放常见的音频文件 343 第4章 控件公共属性、方法及事件 347 4.1 控件公共属性 347 4.1.1 BackColor属性——设置控件的背景色 347 4.1.2 BackgroudColor属性——设置控件背景色 347 4.1.3 BackgroudImage属性——设置控件背景图像 347 4.1.4 Border属性——控件边框 348 4.1.5 BorderStyle属性——控件的边框样式 349 4.1.6 Bottom属性——控件下边缘与其容器的工作区上边缘之间的距离 349 4.1.7 CanFocus属性——控件是否可以接收焦点 350 4.1.8 Capture属性——控件是否已捕获鼠标 350 4.1.9 Color属性——设置用户选定的颜色 350 4.1.10 Dock属性——控件在窗体中的布局样式 351 4.1.11 Enabled属性——控件是否可用 352 4.1.12 ForeColor属性——设置控件的前景色 352 4.1.13 Handle属性——获取控件绑定到的窗口句柄 352 4.1.14 Height属性——设置控件的高度 353 4.1.15 KeyChar属性——设置与按下的键对应的字符 354 4.1.16 KeyValue属性——获取KeyDown或KeyUp事件的键盘值 355 4.1.17 Lines属性——设置多行配置中的文本行 355 4.1.18 Location属性——控件的左上角相对于其容器的左上角的坐标 356 4.1.19 Name属性——控件或实例的名称 356 4.1.20 Parent属性——设置控件的父容器或获取指定子目录的父目录 357 4.1.21 Position属性——设置坐标 358 4.1.22 ReadOnly属性——是否只读 359 4.1.23 Right属性——控件右边缘与其容器的工作区左边缘之间的距离 359 4.1.24 RightToLeft属性——控件的文本从右向左读取 360 4.1.25 ScrollBars属性——滚动条的可见性和位置 360 4.1.26 SizeMode属性——指示如何显示图像 361 4.1.27 Tag属性——窗体或控件的标识 362 4.1.28 Text属性——与控件关联的文本 362 4.1.29 TextAlign 属性——控件上文本的对齐方式 363 4.1.30 Top属性——控件上边缘与其容器的工作区上边缘之间的距离 364 4.1.31 Value属性——辅助性对象的值 364 4.1.32 View属性——项在控件中的显示方式 365 4.1.33 Visible属性——控件是否可见 366 4.1.34 Width属性——控件的宽度 366 4.2 控件公共方法 367 4.2.1 BringToFront方法——将控件带到Z顺序的前面 367 4.2.2 Focus方法——为控件设置输入焦点 367 4.2.3 GetClipboardContent方法——检索选定单元格内容的格式化值 368 4.2.4 GetParent方法——检索指定路径的父目录 368 4.2.5 Hide方法——隐藏窗体 369 4.2.6 Load方法——加载XML文档 369 4.2.7 LoadFile方法——将文件加载到RichTextBox控件中 371 4.2.8 Navigate方法——打开指定的URL地址 372 4.2.9 Refresh方法——重新加载当前的网页 373 4.2.10 SaveAs方法——用新名称或新格式保存文档 373 4.2.11 SaveFile方法——将内容保存到文件中 374 4.2.12 Show方法——显示光标或者打开新窗体 375 4.2.13 UpButton方法——按照指定数值递增 376 4.3 控件公共事件 377 4.3.1 Click事件——单击控件时触发该事件 377 4.3.2 Enter事件——光标进入控件时发生 378 4.3.3 KeyDown事件——控件有焦点按下键时发生 378 4.3.4 KeyPress事件——控件有焦点按下键时发生 380 4.3.5 KeyUp事件——控件有焦点释放键时发生 381 4.3.6 Leave事件——输入焦点离开控件时发生 381 4.3.7 MouseClick事件——用户单击控件时发生 382 4.3.8 Navigated事件——加载新文档时发生 383 4.3.9 Paint事件——重绘或更新控件时发生 383 第5章 数据库开发 385 5.1 SQL语言基础 385 5.1.1 AVG聚合函数——返回组中值的平均值 385 5.1.2 CAST函数——数据类型显式转换 385 5.1.3 COUNT函数——返回组中的项的数量 386 5.1.4 Last函数——返回查询结果的最后一个记录 386 5.1.5 MAX函数——返回表达式中的最大值 388 5.1.6 MIN函数——返回表达式中的最小值 388 5.1.7 newid函数——创建uniqueidentifier类型的惟一值 389 5.1.8 SUM函数——返回表达式中所有值的和 389 5.1.9 UPDATE语句——更改表中的现有数据 390 5.2 ADO.NET技术 392 5.2.1 Command对象——对数据源执行增、删、改、查操作 392 5.2.2 CommandText属性——获取设置SQL语句或存储过程 393 5.2.3 CommandTimeout属性——获取或设置错误等待时间 393 5.2.4 CommandType属性——获取或设置如何解释CommandText属性 394 5.2.5 Connection对象——数据库连接对象 394 5.2.6 ConnectionState枚举——数据库连接状态 395 5.2.7 DataAdapter类——数据库桥接器 396 5.2.8 DataMember属性——获取或设置数据源列表或表名称 398 5.2.9 DataReader类——只读数据集 398 5.2.10 DataSet类——数据集 400 5.2.11 DataSource属性——获取或设置数据源 402 5.2.12 ExecuteNonQuery方法——执行SQL语句并返回受影响的行数 402 5.2.13 ExecuteReader方法——执行SQL语句并返回DataReader对象 403 5.2.14 ExecuteScalar方法——执行SQL语句并返回结果集中第1行的第1列 404 5.2.15 Fill方法——填充数据集 405 5.2.16 Merge方法——合并数据集 407 5.2.17 Parameters属性——获取SqlParameterCollection 409 5.2.18 ReadXml方法——将XML架构和数据读入数据集 410 5.2.19 SelectCommand属性——获取或设置选择记录命令 411 5.2.20 SQL注入式攻击——利用设计上的漏洞攻击SQL 412 5.2.21 SqlCommand类——SQL执行命令 413 5.2.22 SqlConnection类——SQL数据库连接对象 415 5.2.23 SqlDataAdapter类——SQL数据库桥接器 416 5.2.24 SqlDataReader类——SQL只读数据集 418 5.2.25 Tables属性——获取包含在数据集中的表的集合 421 5.2.26 Update方法——使控件重绘工作区内的无效区域 422 5.2.27 UpdateCommand属性——获取或设置更新记录命令 423 5.2.28 WriteXml方法——将数据集中数据写入到XML中 423 5.3 LINQ技术 424 5.3.1 Lambda表达式——匿名函数 424 5.3.2 LINQ技术——语言集成查询 426 5.3.3 LinqToDataSet技术——LINQ操作数据集 427 5.3.4 LinqToObjects技术——LINQ操作数组和集合 429 5.3.5 LinqToSql技术——LINQ操作SQL数据库 431 5.3.6 LinqToXml技术——LINQ操作XML文件 436 5.3.7 var关键字——根据初始化语句推断变量类型 439 第6章 文件、数据流与注册表 441 6.1 文件与I/O数据流 441 6.1.1 ASCII码——键盘的一种表示方式 441 6.1.2 ASCIIEncoding类——ASCII字符编码的操作类 442 6.1.3 Attributes属性——获取和设置文件的属性 443 6.1.4 BinaryReader类——将特定的数据读作二进制值 445 6.1.5 BinaryWriter类——将二进制值写入到流中 447 6.1.6 CanRead属性——判断当前流是否支持读写 448 6.1.7 Close方法——释放所有关联的资源 449 6.1.8 Copy方法——文件的复制 450 6.1.9 CopyFile方法——将文件复制到新的位置 451 6.1.10 CopyTo方法——将指定的字符串复制到字符数组中 452 6.1.11 Create方法——创建文件 455 6.1.12 CreateDirectory方法——创建指定路径中的所有目录 456 6.1.13 CreateText方法——创建或打开文本文件 456 6.1.14 CreationTime属性——获取或设置文件的创建时间 457 6.1.15 CryptoStream类——将数据流连接到加密转换的流 457 6.1.16 Delete方法——删除文件 461 6.1.17 Directory类——对文件夹进行操作的类 463 6.1.18 DirectoryEntry类——封装节点或对象 464 6.1.19 DirectoryInfo类——对文件夹进行操作的类 466 6.1.20 DirectoryName属性——获取路径 468 6.1.21 DirectorySearcher组件——执行查找 468 6.1.22 DriveInfo类——驱动器的信息访问 469 6.1.23 Encoding属性——获取编码方式 470 6.1.24 Exists方法——判断文件是否存在 471 6.1.25 Exists属性——判断文件是否存在 472 6.1.26 Extension属性——获取文件扩展名 473 6.1.27 File类——对文件进行操作的类 473 6.1.28 FileAttributes枚举——提供文件和目录的属性 475 6.1.29 FileInfo类——文件的操作类 476 6.1.30 FileName属性——获取或设置文件的名称 478 6.1.31 FileStream类——对文件流操作的类 478 6.1.32 Flush方法——清除流的缓冲区 480 6.1.33 GetBytes方法——将字符串编码设为字节序列 481 6.1.34 GetDirectories方法——获取子目录的名称 482 6.1.35 GetExtension方法——获取路径字符串的扩展名 485 6.1.36 GetFiles方法——获取目录中的文件名称 486 6.1.37 GetFileSystemEntries方法——获取目录中的所有名称 487 6.1.38 GetFileSystemInfos方法——获取所有文件的信息 489 6.1.39 GetStream方法——返回用于发送和接收的数据 491 6.1.40 GetString方法——将字节解码成字符串 491 6.1.41 HasRows属性——指示 OleDbDataReader是否有数据 493 6.1.42 MD5CryptoServiceProvider类——操作MD5的类 493 6.1.43 MemoryStream类——创建其支持存储区为内存的流 495 6.1.44 Move方法——文件的移动 497 6.1.45 MoveNext方法——移动到下一个字符 497 6.1.46 MoveTo方法——文件的移动 498 6.1.47 NetworkStream类——网络访问的基础数据流 500 6.1.48 Open方法——打开文件 502 6.1.49 OpenFile方法——以只读方式打开文件 503 6.1.50 OpenText方法——打开UTF-8编码文本文件 504 6.1.51 Path属性——监视的目录的路径 505 6.1.52 Peek方法——返回下一个可用的字符 506 6.1.53 Read方法——读取数据流 507 6.1.54 ReadBytes方法——将指定的字节读入字节数组 508 6.1.55 ReadToEnd方法——从流的当前位置读到末尾 509 6.1.56 Stream类——对数据流进行操作的类 510 6.1.57 StreamReader类——数据流的读取类 512 6.1.58 StreamWriter类——数据流的写入类 513 6.1.59 TextReader类——读取连续字符的读取器 515 6.1.60 TextWriter类——编写一个有序字符系列的编写器 516 6.1.61 Write方法——将流写入到文件中 517 6.2 注册表技术 521 6.2.1 CreateSubKey方法——创建或打开子项 521 6.2.2 GetValue方法——获取注册表项中的值 522 6.2.3 GetValueNames方法——所有值名称的字符串数组 523 6.2.4 GetSubKeyNames方法——所有子项名称字符串数组 525 6.2.5 OpenSubKey方法——以只读方式检索子项 525 6.2.6 Registry类——注册表操作类 528 6.2.7 RegistryKey类——表示Windows注册表中的项级节点 529 6.2.8 SetValue方法——设置注册表项的指定名称/值对 531 第7章 GDI+绘图技术 532 7.1 GDI+绘图基础 532 7.1.1 Bitmap类——图像对象 532 7.1.2 Cursor类——绘制光标指针图像 533 7.1.3 GDI+——图形图像的绘制 535 7.1.4 Graphics类——绘图类 536 7.1.5 GraphicsPath类——一系列相互连接的直线和曲线 540 7.1.6 Icon类——图标的操作类 542 7.1.7 Image类——图像的操作类 543 7.1.8 LinearGradientBrush类——线性渐变封装Brush 545 7.1.9 Region类——由矩形和路径构成的图形形状的内部 547 7.1.10 SolidBrush类——定义单色画笔 548 7.2 常用绘图方法 549 7.2.1 Draw方法——绘制光标 549 7.2.2 DrawArc方法——绘制圆弧 550 7.2.3 DrawBezier方法——绘制贝塞尔样条 551 7.2.4 DrawEllipse方法——绘制椭圆 553 7.2.5 DrawImage方法——绘制Image图像 555 7.2.6 DrawLine方法——绘制直线 556 7.2.7 DrawPath方法——绘制GraphicsPath图形路径 558 7.2.8 DrawPie方法——绘制扇形 558 7.2.9 DrawPolygon方法——绘制多边形 560 7.2.10 DrawRectangle方法——绘制矩形 561 7.2.11 DrawString方法——绘制文本字符串 562 7.3 常用填充图像方法 565 7.3.1 FillEllipse方法——填充椭圆 565 7.3.2 FillPath方法——填充GraphicsPath的内部 566 7.3.3 FillPie方法——填充扇形 567 7.3.4 FillPolygon方法——填充多边形 568 7.3.5 FillRectangle方法——填充矩形框 570 7.3.6 FillRegion方法——填充一个区域 572 7.4 其他常用方法 572 7.4.1 Clone方法——创建Bitmap对象的某个部分的副本 572 7.4.2 CreateGraphics方法——创建Graphics对象 574 7.4.3 FromArgb方法——从ARGB值创建Color结构 574 7.4.4 FromFile方法——从指定的文件创建Image 577 7.4.5 FromImage方法——从Image创建新的Graphics对象 578 7.4.6 FromStream方法——数据流创建Image 578 7.4.7 GetPixel方法——获取图像中的像素颜色 580 7.4.8 GetThumbnailImage方法——Image的缩略图 581 7.4.9 Save方法——将图片以文件的形式进行复制 583 7.4.10 SetPixel方法——设置图像中的像素颜色 583 7.4.11 Transform方法——对路径的数据点进行变换 584 第8章 C#高级编程 586 8.1 网络编程技术 586 8.1.1 Accept方法——为新建连接创建新的Socket对象 586 8.1.2 AcceptSocket方法——接收挂起的连接请求 586 8.1.3 BeginConnect方法——开始远程主机连接的异步请求 587 8.1.4 Dns类——从Internet域名系统检索特定主机的信息 588 8.1.5 GetHostAddresses方法——返回主机的IP地址 589 8.1.6 GetHostByAddress方法——创建IPHostEntry实例 590 8.1.7 GetHostByName方法——获取指定DNS主机名的信息 591 8.1.8 GetHostName方法——获取本地计算机的主机名 592 8.1.9 IPEndPoint类——将网络端点表示为IP地址和端口号 592 8.1.10 IPHostEntry类——为主机地址信息提供容器类 594 8.1.11 Listen方法——将Socket置于侦听状态 596 8.1.12 MachineName属性——读取或写入事件的计算机名称 596 8.1.13 MailMessage类——邮件的操作类 597 8.1.14 Net send命令——用net send命令进行发送 598 8.1.15 Net use命令——实现映射网络驱动器 599 8.1.16 Ping类——网络访问远程计算机的操作类 601 8.1.17 POP3协议——POP邮件的操作类 603 8.1.18 Receive方法——由远程主机发送的UDP数据报 608 8.1.19 Send方法——将数据发送到连接的Socket 609 8.1.20 SerialPort类——控制串行端口文件资源 610 8.1.21 SMTP协议——进行邮件的传输 612 8.1.22 SmtpClient类——将电子邮件发送到SMTP服务器 614 8.1.23 Socket类——网络通信的操作类 616 8.1.24 TcpClient类——为TCP网络服务提供客户端连接 618 8.1.25 TcpListener类——从TCP网络客户端侦听连接 619 8.1.26 UdpClient类——用户数据报(UDP)网络服务 620 8.1.27 WebClient类——URI标识的资源发送和接收 623 8.1.28 WebRequest类——访问Internet数据 625 8.1.29 WebResponse类——协议特定的响应类 629 8.2 多线程编程 630 8.2.1 Abort方法——终止线程 630 8.2.2 BeginInvoke方法——线程上异步执行委托 631 8.2.3 EndInvoke方法——异步操作的返回值 632 8.2.4 Join方法——确保线程已终止 633 8.2.5 Kill方法——强制关闭进程 633 8.2.6 Process类——对正在计算机上运行的进程的访问 635 8.2.7 Sleep方法——线程挂起 640 8.2.8 Start方法——启动进程 640 8.2.9 Thread类——创建并控制线程的类 642 8.2.10 ThreadState属性——获取当前线程的状态 645 8.3 WMI技术——系统管理 646 8.3.1 MainWindowTitle属性——获取进程的主窗口标题 646 8.3.2 ManagementClass类——公共信息模型管理类 647 8.3.3 ManagementObject类——表示WMI实例 648 8.3.4 ManagementObjectSearcher类——查询检索管理对象 650 8.3.5 ManagementScope类——管理操作的范围 651 8.3.6 Microsoft.Win32命名空间——操作注册表类 652 8.3.7 WndProc方法——处理Windows消息 654 8.4 其他高级技术 655 8.4.1 Children属性——获取节点的子项 655 8.4.2 COM+服务——为类的实例提供服务 655 8.4.3 DirectShow技术——流媒体处理的一个开发包 656 8.4.4 DLL组件——动态链接库 663 8.4.5 MVC开发模式——模型视图控制器 664 8.4.6 VFW技术——视频应用程序提供的软件工具包 666 8.4.7 XML——定义其他标识语言的元标识语言 668 8.4.8 XmlReader类——XML读取器 670 8.4.9 XmlWriter类——XML编写器 673 附录——字母索引 676
/* API精灵 FOR C# 开始设计日期 2004.03.06 设计目的:简单快速对C#中使用的API函数进行查询,并给出调用代码 设计进度: 2004.03.09 完成对的查询功能,包括 代码调用,中文注释,所需的DLL库,与C#中函数对应关系 2004.03.10 0:48:52 完成了用StringBuilder数组对原ComboBox的替换,可以使程序不用从新读取数据库就可以刷新修改后的信息! 2004.03.10 18:00:00 完成了用ArrayList对StringBuilder数组的替换节省2M内存 2004.03.11 21:10:15 完成滚动字幕的设置,启用了一个TIMER控件,然后设置时间,删除字符串的第一个字母已达到滚动效果! 2004.03.11 22:02:00 改正更新时出现空值出错问题,新填函数isnull 2004.03.12 13:22:08 完成关键字高亮显示 高亮显示函数 mykeywords 2004.03.12 22:08:20 加强了高亮显示函数 mykeywords的功能,使其能识别不同的关键字并显示不同的颜色 2004.03.14 01:40:00 完成对CONST的查询,并且增加了 mykeywords1函数,使其关键字显示性能提高 2004.03.14 13:12:00 添加了提示信息,提示信息设置在函数 mytips() 中 2004.03.15 21:51:20 更改数据库和WINAPI.TXT路径为程序运行路径 2004.03.15 22:31:50 添加了鼠标右键信息 2004.03.15 23.23:30 添加了数据库密码 2004.03.16 23:24:30 添加了版权信息以及相应提示 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Runtime.InteropServices; using System.IO; namespace API精灵 { /// /// Form1 的摘要说明。 /// 这个版本没有使用oleDbDataAdapter+DataSet对数据进行存取,而是使用的OleDbCommand +OleDbDataReader 的形式。 /// 主要是想试验一下不用oleDbDataAdapter+DataSet的数据存取速度。 /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Button button4; private System.Windows.Forms.TextBox mysearch; private System.Windows.Forms.ListBox tiplist; private System.Windows.Forms.ComboBox select_type; private System.Windows.Forms.TextBox dlltext; /// /// 必需的设计器变量。 /// //自定义变量 private ArrayList fundll = new ArrayList();//保存读取出来的DLL内容 private ArrayList funtips = new ArrayList();//保存读取出来的中文提示信息 private ArrayList funcode = new ArrayList();//保存读取出来的C#调用代码 private ArrayList funmat = new ArrayList();//保存读取出来的C#对应函数 private ArrayList funwin9x = new ArrayList();//保存读取出来的WIN9X private ArrayList funwin2k = new ArrayList();//保存读取出来的WIN2K private int nowselect = 0; private string oldscoll_text; private int nowtypeselect = 0; private string nowpath = @System.Environment.CurrentDirectory+@"\"; private string dbpassword = "ling_feng_work"; public string myConnstr; public OleDbConnection myconn ;//创建一个新连接 private string mysql ;//查询语句 private string sql_update; private System.Windows.Forms.RichTextBox tipsmemo; private System.Windows.Forms.TextBox mat_text; private OleDbCommand mycommand = new OleDbCommand(); private System.Windows.Forms.RichTextBox codememo; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.CheckBox win9x; private System.Windows.Forms.GroupBox groupBox5; private System.Windows.Forms.CheckBox win2k; private System.Windows.Forms.CheckBox e_add; private System.Windows.Forms.CheckBox e_modify; private System.Windows.Forms.Button b_modify; private System.Windows.Forms.Button b_add; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.ContextMenu mypop; private System.Windows.Forms.MenuItem menuItem1; private System.ComponentModel.IContainer components; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.button1 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.tiplist = new System.Windows.Forms.ListBox(); this.select_type = new System.Windows.Forms.ComboBox(); this.mat_text = new System.Windows.Forms.TextBox(); this.mysearch = new System.Windows.Forms.TextBox(); this.dlltext = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.tipsmemo = new System.Windows.Forms.RichTextBox(); this.mypop = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.codememo = new System.Windows.Forms.RichTextBox(); this.b_modify = new System.Windows.Forms.Button(); this.b_add = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.win2k = new System.Windows.Forms.CheckBox(); this.win9x = new System.Windows.Forms.CheckBox(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.e_add = new System.Windows.Forms.CheckBox(); this.e_modify = new System.Windows.Forms.CheckBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.groupBox5.SuspendLayout(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button1.Location = new System.Drawing.Point(224, 395); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 24); this.button1.TabIndex = 10; this.button1.Text = "关 于"; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.tiplist); this.groupBox1.Controls.Add(this.select_type); this.groupBox1.Controls.Add(this.mat_text); this.groupBox1.Controls.Add(this.mysearch); this.groupBox1.Controls.Add(this.dlltext); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 168); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "API查询"; // // tiplist // this.tiplist.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tiplist.ItemHeight = 12; this.tiplist.Location = new System.Drawing.Point(8, 43); this.tiplist.Name = "tiplist"; this.tiplist.Size = new System.Drawing.Size(184, 110); this.tiplist.TabIndex = 1; this.tiplist.Visible = false; this.tiplist.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tiplist_KeyDown); this.tiplist.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tiplist_KeyPress); this.tiplist.DoubleClick += new System.EventHandler(this.tiplist_DoubleClick); this.tiplist.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tiplist_MouseUp); this.tiplist.MouseLeave += new System.EventHandler(this.tiplist_MouseLeave); this.tiplist.SelectedIndexChanged += new System.EventHandler(this.tiplist_SelectedIndexChanged); // // select_type // this.select_type.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.select_type.Items.AddRange(new object[] { "API函数查询", "常量定义查询"}); this.select_type.Location = new System.Drawing.Point(8, 61); this.select_type.Name = "select_type"; this.select_type.Size = new System.Drawing.Size(184, 20); this.select_type.TabIndex = 2; this.select_type.SelectedIndexChanged += new System.EventHandler(this.select_type_SelectedIndexChanged); // // mat_text // this.mat_text.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.mat_text.Location = new System.Drawing.Point(8, 134); this.mat_text.Name = "mat_text"; this.mat_text.Size = new System.Drawing.Size(184, 21); this.mat_text.TabIndex = 4; this.mat_text.Text = "C#对应函数:"; this.mat_text.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mat_text_MouseDown); // // mysearch // this.mysearch.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.mysearch.Location = new System.Drawing.Point(8, 24); this.mysearch.Name = "mysearch"; this.mysearch.Size = new System.Drawing.Size(184, 21); this.mysearch.TabIndex = 0; this.mysearch.Text = ""; this.mysearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mysearch_KeyDown); this.mysearch.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mysearch_MouseDown); this.mysearch.TextChanged += new System.EventHandler(this.mysearch_TextChanged); // // dlltext // this.dlltext.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.dlltext.Location = new System.Drawing.Point(8, 97); this.dlltext.Name = "dlltext"; this.dlltext.Size = new System.Drawing.Size(184, 21); this.dlltext.TabIndex = 3; this.dlltext.Text = ""; this.dlltext.TextChanged += new System.EventHandler(this.dlltext_TextChanged); // // groupBox2 // this.groupBox2.Controls.Add(this.tipsmemo); this.groupBox2.Location = new System.Drawing.Point(216, 8); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(200, 168); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "函数注释"; // // tipsmemo // this.tipsmemo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.tipsmemo.ContextMenu = this.mypop; this.tipsmemo.Location = new System.Drawing.Point(8, 16); this.tipsmemo.Name = "tipsmemo"; this.tipsmemo.Size = new System.Drawing.Size(184, 144); this.tipsmemo.TabIndex = 0; this.tipsmemo.Text = ""; this.tipsmemo.MouseEnter += new System.EventHandler(this.richTextBox1_MouseEnter); // // mypop // this.mypop.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "复制信息"; this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // // groupBox3 // this.groupBox3.Controls.Add(this.codememo); this.groupBox3.Location = new System.Drawing.Point(8, 221); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(408, 168); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "代码调用"; // // codememo // this.codememo.ContextMenu = this.mypop; this.codememo.Location = new System.Drawing.Point(8, 16); this.codememo.Name = "codememo"; this.codememo.Size = new System.Drawing.Size(392, 144); this.codememo.TabIndex = 0; this.codememo.Text = ""; this.codememo.TextChanged += new System.EventHandler(this.codememo_TextChanged); // // b_modify // this.b_modify.Enabled = false; this.b_modify.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.b_modify.Location = new System.Drawing.Point(120, 395); this.b_modify.Name = "b_modify"; this.b_modify.Size = new System.Drawing.Size(75, 24); this.b_modify.TabIndex = 4; this.b_modify.Text = "修改信息"; this.b_modify.Click += new System.EventHandler(this.button2_Click); // // b_add // this.b_add.Enabled = false; this.b_add.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.b_add.Location = new System.Drawing.Point(16, 395); this.b_add.Name = "b_add"; this.b_add.Size = new System.Drawing.Size(75, 24); this.b_add.TabIndex = 5; this.b_add.Text = "添加新项"; // // button4 // this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button4.Location = new System.Drawing.Point(328, 395); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(75, 24); this.button4.TabIndex = 6; this.button4.Text = "退 出"; this.button4.Click += new System.EventHandler(this.button4_Click); // // groupBox4 // this.groupBox4.Controls.Add(this.win2k); this.groupBox4.Controls.Add(this.win9x); this.groupBox4.Location = new System.Drawing.Point(8, 176); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(200, 40); this.groupBox4.TabIndex = 11; this.groupBox4.TabStop = false; // // win2k // this.win2k.Location = new System.Drawing.Point(104, 11); this.win2k.Name = "win2k"; this.win2k.Size = new System.Drawing.Size(80, 24); this.win2k.TabIndex = 1; this.win2k.Text = "支持win2k"; // // win9x // this.win9x.Location = new System.Drawing.Point(16, 11); this.win9x.Name = "win9x"; this.win9x.TabIndex = 0; this.win9x.Text = "支持win9x"; // // groupBox5 // this.groupBox5.Controls.Add(this.e_add); this.groupBox5.Controls.Add(this.e_modify); this.groupBox5.Location = new System.Drawing.Point(216, 176); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(200, 40); this.groupBox5.TabIndex = 12; this.groupBox5.TabStop = false; // // e_add // this.e_add.Location = new System.Drawing.Point(112, 10); this.e_add.Name = "e_add"; this.e_add.Size = new System.Drawing.Size(80, 24); this.e_add.TabIndex = 5; this.e_add.Text = "允许添加"; this.e_add.CheckedChanged += new System.EventHandler(this.e_add_CheckedChanged); // // e_modify // this.e_modify.Location = new System.Drawing.Point(16, 10); this.e_modify.Name = "e_modify"; this.e_modify.Size = new System.Drawing.Size(76, 24); this.e_modify.TabIndex = 4; this.e_modify.Text = "允许修改"; this.e_modify.CheckedChanged += new System.EventHandler(this.e_modify_CheckedChanged); // // timer1 // this.timer1.Interval = 500; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(424, 429); this.Controls.Add(this.groupBox5); this.Controls.Add(this.groupBox4); this.Controls.Add(this.button4); this.Controls.Add(this.b_add); this.Controls.Add(this.b_modify); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox1); this.Controls.Add(this.button1); this.Controls.Add(this.groupBox2); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "API精灵FOR C#"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.groupBox4.ResumeLayout(false); this.groupBox5.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new Form1()); } [DllImport("user32.dll", EntryPoint="ShowWindow")] public static extern int ShowWindow ( int hwnd, int nCmdShow ); private void button1_Click(object sender, System.EventArgs e) { AboutForm myabout = new AboutForm(); myabout.ShowDialog(); } /// /// 填写mysearch中的内容。 /// private void search_comp() { if (tiplist.SelectedIndex>-1) mysearch.Text = tiplist.SelectedItem.ToString(); tiplist.Visible = false ; mysearch.Select(); } /// /// 自动填写提示内容。 /// private void autocomp() { if (tiplist.SelectedIndex>-1) try { if (this.nowtypeselect==0) //================查询函数 { this.nowselect = tiplist.SelectedIndex; dlltext.Text = fundll[tiplist.SelectedIndex].ToString();//else tipsmemo.Text = funtips[tiplist.SelectedIndex].ToString(); codememo.Text = funcode[tiplist.SelectedIndex].ToString(); mat_text.Text = funmat[tiplist.SelectedIndex].ToString(); this.oldscoll_text = mat_text.Text; if (funwin9x[tiplist.SelectedIndex].ToString()==("Yes")) win9x.Checked=true; else win9x.Checked=false; if (funwin2k[tiplist.SelectedIndex].ToString()==("Yes")) win2k.Checked=true; else win2k.Checked=false; //滚动文字 if (mat_text.TextLength>30) timer1.Enabled = true; else timer1.Enabled = false; } else { dlltext.Text = ""; tipsmemo.Text = ""; codememo.Text = ""; win9x.Checked = false; win2k.Checked = false; } //******************** if (this.nowtypeselect==1) { this.nowselect = tiplist.SelectedIndex; codememo.Text = funcode[tiplist.SelectedIndex].ToString(); } //******************** //================= } catch { dlltext.Text = "没有找到相应连接库"; tipsmemo.Text = "没有找到相应提示"; codememo.Text = "没有找到相应调用代码"; mat_text.Text = "没有找到相应C#函数"; } } // /// /// 手动释放一些内存。 /// private void mydisp() { tipsmemo.Clear(); codememo.Clear(); tiplist.Items.Clear(); // ===== fundll.Clear();//保存读取出来的DLL内容 funtips.Clear();//保存读取出来的中文提示信息 funcode.Clear();//保存读取出来的C#调用代码 funmat.Clear();//保存读取出来的C#对应函数 funwin9x.Clear();//保存读取出来的WIN9X funwin2k.Clear();//保存读取出来的WIN2K } private void mysearch_TextChanged(object sender, System.EventArgs e) { tiplist.Visible = true ; //自动完成功能。 tiplist.SelectedIndex = (tiplist.FindString(mysearch.Text,-1)) ;//加上这句,保证TIPLIST跟着自动变化 nowselect = tiplist.SelectedIndex; autocomp(); //设置提示信息 mytips(); } private void mysearch_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up)) tiplist.Focus(); if (e.KeyCode == Keys.Enter) { search_comp(); if (this.nowtypeselect==0) mykeyword(); if (this.nowtypeselect==1) mykeyword1(); } if (e.KeyCode == Keys.Escape) { tiplist.Visible = false ; } } private void tiplist_MouseLeave(object sender, System.EventArgs e) { } private void tiplist_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { } private void tiplist_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.search_comp(); if (this.nowtypeselect==0) mykeyword(); else mykeyword1(); } if (e.KeyCode == Keys.Escape) { tiplist.Visible = false ; } } private void tiplist_DoubleClick(object sender, System.EventArgs e) { this.search_comp(); if (this.nowtypeselect==0) mykeyword(); else mykeyword1(); } private void Form1_Load(object sender, System.EventArgs e) { //初始化数据库 initdatabase(); //MessageBox.Show(this,"欢迎使用 共享版\n 本版对使用功能上略有限制\n 且不提供数据库更新!\n 如想获得更多信息请与我联系。\n dong_teng@tom.com","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); select_type.SelectedIndex = 0; AboutForm myabout = new AboutForm(); myabout.ShowDialog(); } // private void mytips() { //设置提示信息 if ((this.nowselect>-1)&(this.nowtypeselect==0)) { toolTip1.SetToolTip(this.dlltext,"所在动态连接库: "+this.fundll[this.nowselect].ToString()); toolTip1.SetToolTip(this.mat_text,"在C#中对应的函数: "+this.funmat[this.nowselect].ToString()); toolTip1.SetToolTip(this.codememo,"函数 "+this.tiplist.SelectedItem.ToString()+" 在C#中的调用代码,可以手动修改"); toolTip1.SetToolTip(this.tipsmemo,"函数 "+this.tiplist.SelectedItem.ToString()+" 的注释信息,可以手动修改"); } else if ((this.nowselect>-1)&(this.nowtypeselect==1)) { toolTip1.SetToolTip(this.codememo,"常量 "+this.tiplist.SelectedItem.ToString()+" 在C#中的调用代码"); toolTip1.SetToolTip(this.dlltext,"没有相关信息"); toolTip1.SetToolTip(this.mat_text,"没有相关信息"); toolTip1.SetToolTip(this.tipsmemo,"没有相关信息"); } } // private void initdatabase() { string dbpath = @nowpath+"winapi.mdb"; tiplist.Items.Clear(); //@"Provider=Microsoft.Jet.OleDB.4.0;Data Source="+dbpath+";User Id=admin;Password="+this.dbpassword ; //"Provider=Microsoft.Jet.OleDB.4.0;Data Source=your mdb filename;Jet OLEDB:Database Password='pass'" ; this.myConnstr = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source="+dbpath+";User Id=admin;Jet OLEDB:Database Password="+this.dbpassword ; this.myconn= new OleDbConnection(myConnstr); mysql= @"select Fun_name,Fun_dll,Fun_tips,Fun_code,Fun_com,win9x,win2k from winapi"; using(myconn) { myconn.Open(); // if (myconn.State.ToString() == "Open") MessageBox.Show("打开成功!"); //数据处理 // OleDbCommand mycommand = new OleDbCommand(mysql,myconn); mycommand.CommandText = mysql; mycommand.Connection = myconn; OleDbDataReader myreader = mycommand.ExecuteReader(); int i=0; while (myreader.Read()) { tiplist.Items.Add(myreader["Fun_name"]); fundll.Add(myreader["fun_dll"].ToString()); funtips.Add(myreader["fun_tips"].ToString()); funcode.Add(myreader["fun_code"].ToString()); funmat.Add(myreader["fun_com"].ToString()); funwin9x.Add(myreader["win9x"].ToString()); funwin2k.Add(myreader["win2k"].ToString()); i++; } myconn.Close(); myreader.Close(); } } //更新缓存 private void memo_update() { fundll[nowselect] = dlltext.Text; funtips[nowselect] = this.tipsmemo.Text; funcode[nowselect] = this.codememo.Text; funmat[nowselect] = this.mat_text.Text; if (win9x.Checked) funwin9x[nowselect]="Yes" ;else funwin9x[nowselect]="No"; if (win2k.Checked) funwin2k[nowselect]="Yes" ;else funwin2k[nowselect]="No"; } // private void oleDbConnection1_InfoMessage(object sender, System.Data.OleDb.OleDbInfoMessageEventArgs e) { } //==============从WINAPI.TXT读取CONST并拆分 private void mysplip() { //string dbpath = @System.Environment.CurrentDirectory+@"\winapi.mdb"; string filename =@nowpath +"winapi.txt"; string nextline; tiplist.Items.Clear(); StreamReader sr = new StreamReader(filename); while ((nextline = sr.ReadLine())!=null) { if (nextline.StartsWith("public const")) { string[] ss = nextline.Split('='); tiplist.Items.Add(ss[0].Substring(16).Trim()); funcode.Add(nextline); } } sr.Close(); } //============================== private void select_type_SelectedIndexChanged(object sender, System.EventArgs e) { if (select_type.SelectedIndex != this.nowtypeselect) { this.mydisp(); switch (select_type.SelectedIndex) { case 0:initdatabase();this.nowtypeselect=select_type.SelectedIndex;break; case 1:mysplip();this.nowtypeselect=select_type.SelectedIndex;this.dlltext.Clear();this.mat_text.Clear();break; } isenable(this.nowtypeselect); } } private void dlltext_TextChanged(object sender, System.EventArgs e) { } private void richTextBox1_MouseEnter(object sender, System.EventArgs e) { tiplist.Visible = false ; } private void mysearch_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { tiplist.Visible = false ; mysearch.Focus(); } private void tiplist_SelectedIndexChanged(object sender, System.EventArgs e) { autocomp(); } /// /// 修改内容。 /// private void fun_update() { using(myconn) { myconn.ConnectionString = myConnstr; myconn.Open(); try { //if (myconn.State.ToString() == "Open") MessageBox.Show("打开成功!"); isnull();//判断是否有无效值 string str_win9x,str_win2k; if (win9x.Checked) str_win9x = "Yes" ; else str_win9x = "No"; if (win2k.Checked) str_win2k = "Yes" ; else str_win2k = "No"; sql_update = "update winapi set Fun_dll = '"+dlltext.Text+"'"+" , Fun_tips = '"+tipsmemo.Text+"'"+" , Fun_code = '"+codememo.Text+"'"+" , Fun_com ='"+mat_text.Text+"' "; sql_update +=" , win9x = '" + str_win9x +"' " + ", win2k = '" + str_win2k+"' "; sql_update +=" where Fun_name ='"+ mysearch.Text+"'"; mycommand.Connection = myconn; mycommand.CommandText = sql_update; mycommand.ExecuteNonQuery(); myconn.Close(); memo_update(); MessageBox.Show(this,"恭喜!更新成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch { // tipsmemo.Text = sql_update; MessageBox.Show("没有找到相应记录,更新失败!"); } } } //判断是更新的部分是否有效(不能为空) private void isnull() { if (this.mat_text.Text=="") this.mat_text.Text="没有相关信息"; if (this.codememo.Text=="") this.codememo.Text="没有相关信息"; if (this.tipsmemo.Text=="") this.tipsmemo.Text="没有相关信息"; if (this.dlltext.Text=="") this.dlltext.Text="没有相关信息"; } //关键字高亮显示 private void mykeyword() { string[] keywords = new string[5]; keywords[0]=mysearch.Text; keywords[1]="string"; keywords[2]="ref"; keywords[3]="int"; keywords[4]="static extern"; for(int i=0;i0) { index++; switch(i) { case 0: codememo.SelectionColor = Color.Red;break; case 1: codememo.SelectionColor = Color.Green;break; case 2: codememo.SelectionColor = Color.Brown;break; case 3: codememo.SelectionColor = Color.Blue;break; case 4: codememo.SelectionColor = Color.Green;break; //default:codememo.SelectionColor = Color.Blue;break; } } } } // //CONST中关键字高亮显示 private void mykeyword1() { string[] keywords = new string[5]; keywords[0]=mysearch.Text; keywords[1]="="; keywords[2]="0"; keywords[3]="int"; keywords[4]="const"; for(int i=0;i0) { index++; switch(i) { case 0: codememo.SelectionColor = Color.Red;break; case 1: codememo.SelectionColor = Color.Blue;break; case 2: codememo.SelectionColor = Color.Green;break; case 3: codememo.SelectionColor = Color.Blue;break; case 4: codememo.SelectionColor = Color.Green;break; //default:codememo.SelectionColor = Color.Blue;break; } if (index>codememo.TextLength) break; } } } // private void button2_Click(object sender, System.EventArgs e) { this.fun_update(); } private void isenable(int i_temp) { if (i_temp==0) { win9x.Enabled=true; win2k.Enabled=true; e_modify.Enabled=true; e_add.Enabled=true; b_modify.Enabled=true; b_add.Enabled=true; } else { win9x.Enabled=false; win2k.Enabled=false; e_modify.Enabled=false; e_add.Enabled=false; b_modify.Enabled=false; b_add.Enabled=false; } } private void button4_Click(object sender, System.EventArgs e) { Application.Exit(); } private void e_modify_CheckedChanged(object sender, System.EventArgs e) { if (e_modify.Checked) b_modify.Enabled = true; else b_modify.Enabled = false; } private void e_add_CheckedChanged(object sender, System.EventArgs e) { if (e_add.Checked) b_add.Enabled = true; else b_add.Enabled = false; } private void timer1_Tick(object sender, System.EventArgs e) { if (this.mat_text.TextLength>0) mat_text.Text = mat_text.Text.Remove(0,1); else mat_text.Text = oldscoll_text; } private void mat_text_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { timer1.Enabled = false ; mat_text.Text = oldscoll_text; } private void codememo_TextChanged(object sender, System.EventArgs e) { } private void tiplist_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { } private void menuItem1_Click(object sender, System.EventArgs e) { Control ct = this.ActiveControl; string temp = ct.Text; Clipboard.SetDataObject(temp); } private void menuItem2_Click(object sender, System.EventArgs e) { } } }

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧