毕业设计翻译文稿-Create a Windows-Based Application from the Command Line_第1页
毕业设计翻译文稿-Create a Windows-Based Application from the Command Line_第2页
毕业设计翻译文稿-Create a Windows-Based Application from the Command Line_第3页
毕业设计翻译文稿-Create a Windows-Based Application from the Command Line_第4页
毕业设计翻译文稿-Create a Windows-Based Application from the Command Line_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Create a Windows-Based Application from the Command Line(题目)ProblemYou need to use the C# command-line compiler to build an application that provides a Windows Formsbased GUI.SolutionCreate a class that extends the System.Windows.Forms.Form class. (This will be your applications main form.) In one o

2、f your classes, ensure you implement a static method named Main. In the Main method, create an instance of your main form class and pass it to the static method Run of the System.Windows.Forms.Application class. Build your application using the command-line C# compiler, and specify the /target:winex

3、e compiler switch.Note If you own Visual Studio, you will most often use the Windows Application project template to create new Windows Formsbased applications. Building large GUI-based applications is a time-consuming undertaking that involves the correct instantiation, configuration, and wiring up

4、 of many forms and controls. Visual Studio automates much of the work associated with building graphical applications. Trying to build a large graphical application without the aid of tools such as Visual Studio will take you much longer, be extremely tedious, and result in a greater chance of bugs

5、in your code. However, it is also useful to know the essentials required to create a Windows-based application using the command line in case you are ever working on a machine without Visual Studio and want to create a quick utility to automate some task or get input from a user.How It WorksBuilding

6、 an application that provides a simple Windows GUI is a world away from developing a full-fledged Windows-based application. However, you must perform certain tasks regardless of whether you are writing the Windows equivalent of Hello World or the next version of Microsoft Word, including the follow

7、ing: For each form you need in your application, create a class that extends the System.Windows.Forms.Form class. In each of your form classes, declare members that represent the controls that will be on that form, such as buttons, labels, lists, and textboxes. These members should be declared priva

8、te or at least protected so that other program elements cannot access them directly. If you need to expose the methods or properties of these controls, implement the necessary members in your form class, providing indirect and controlled access to the contained controls. Declare methods in your form

9、 class that will handle events raised by the controls contained by the form, such as button clicks or key presses when a textbox is the active control. These methods should be private or protected and follow the standard .NET event pattern (described in recipe 13-11). Its in these methods (or method

10、s called by these methods) where you will define the bulk of your applications functionality. Declare a constructor for your form class that instantiates each of the forms controls and configures their initial state (size, color, position, content, and so on). The constructor should also wire up the

11、 appropriate event handler methods of your class to the events of each control. Declare a static method named Mainusually as a member of your applications main form class. This method is the entry point for your application, and it can have the same signatures as those mentioned in recipe 1-1. In th

12、e Main method, call Application.EnableVisualStyles to allow XP theme support, create an instance of your applications main form, and pass it as an argument to the static Application.Run method. The Run method makes your main form visible and starts a standard Windows message loop on the current thre

13、ad, which passes the user input (key presses, mouse clicks, and so on) to your application form as events.Connect to a DatabaseProblemYou need to open a connection to a database.SolutionCreate a connection object appropriate to the type of database to which you need to connect. All connection object

14、s implement the System.Data.IDbConnection interface. Configure the connection object by setting its ConnectionString property. Open the connection by calling the connection objects Open method.How It WorksThe first step in database access is to open a connection to the database. The IDbConnection in

15、terface represents a database connection, and each data provider includes a unique implementation. Here is the list of IDbConnection implementations for the five standard data providers: System.Data.Odbc.OdbcConnection System.Data.OleDb.OleDbConnection System.Data.OracleClient.OracleConnection Syste

16、m.Data.SqlServerCe.SqlCeConnection System.Data.SqlClient.SqlConnectionYou configure a connection object using a connection string. A connection string is a set of semicolon-separated name-value pairs. You can supply a connection string either as a constructor argument or by setting a connection obje

17、cts ConnectionString property before opening the connection.Each connection class implementation requires that you provide different information in the connection string. Refer to the ConnectionString property documentation for each implementation to see the values you can specify. Possible settings

18、 include the following: The name of the target database server The name of the database to open initially Connection time-out values Connection-pooling behavior (see recipe 9-2) Authentication mechanisms to use when connecting to secured databases, including provision of a username and password if n

19、eeded .Once configured, call the connection objects Open method to open the connection to the database. You can then use the connection object to execute commands against the data source (discussed in recipe 9-3). The properties of a connection object also allow you to retrieve information about the

20、 state of a connection and the settings used to open the connection. When youre finished with a connection, you should always call its Close method to free the underlying database connection and system resources. IDbConnection extends System.IDisposable, meaning that each connection class implements

21、 the Dispose method. Dispose automatically calls Close, making the using statement a very clean and efficient way of using connection objects in your code.You achieve optimum scalability by opening your database connection as late as possible and closing it as soon as you have finished. This ensures

22、 that you do not tie up database connections for long periods, so you give all code the maximum opportunity to obtain a connection. This is especially important if you are using connection pooling.Perform Asynchronous Database Operations Against SQL ServerProblemYou need to execute a query or comman

23、d against a SQL Server database as a background task while your application continues with other processing.SolutionUse the BeginExecuteNonQuery, BeginExecuteReader, or BeginExecuteXmlReader method of the System.Data.SqlClient.SqlCommand class to start the database operation as a background task. Th

24、ese methods all return a System.IAsyncResult object that you can use to determine the operations status or use thread synchronization to wait for completion. Use the IAsyncResult object and the corresponding EndExecuteNonQuery, EndExecuteReader, or EndExecuteXmlReader method to obtain the result of

25、the operation.Note Only the SqlCommand class supports the asynchronous operations described in this recipe. The equivalent command classes for the Oracle, SQL Server CE, ODBC, and OLE DB data providers do not provide this functionality.How It WorksYou will usually execute operations against database

26、s synchronously, meaning that the calling codeblocks until the operation is complete. Synchronous calls are most common because your code will usually require the result of the operation before it can continue. However, sometimes its useful to execute a database operation asynchronously, meaning tha

27、t you start the method in a separate thread and then continue with other operations.Note To execute asynchronous operations over a System.Data.SqlClient.SqlConnection connection, you must specify the value Asynchronous Processing=true in its connection string. As of .NET Framework 2.0, the SqlComman

28、d class implements the asynchronous execution pattern similar to that discussed in recipe 4-2. As with the general asynchronous execution pattern described in recipe 4-2, the arguments of the asynchronous execution methods (BeginExecuteNonQuery, BeginExecuteReader, and BeginExecuteXmlReader) are the

29、 same as those of the synchronous variants (ExecuteNonQuery, ExecuteReader, and ExecuteXmlReader), but they take the following two additional arguments to support asynchronous completion: A System.AsyncCallback delegate instance that references a method that the runtime will call when the asynchrono

30、us operation completes. The method is executed in the context of a thread-pool thread. Passing null means that no method is called and you must use another completion mechanism (discussed later in this recipe) to determine when the asynchronous operation is complete. An object reference that the run

31、time associates with the asynchronous operation. The asynchronous operation does not use nor have access to this object, but its available to your code when the operation completes, allowing you to associate useful state information with an asynchronous operation. For example, this object allows you

32、 to map results against initiated operations in situations where you initiate many asynchronous operations that use a common callback method to perform completion.The EndExecuteNonQuery, EndExecuteReader, and EndExecuteXmlReader methods allow you to retrieve the return value of an operation that was

33、 executed asynchronously, but you must first determine when it has finished. Here are the four techniques for determining if an asynchronous method has finished: Blocking: This method stops the execution of the current thread until the asynchronous operation completes execution. In effect, this is m

34、uch the same as synchronous execution. However, you do have the flexibility to decide exactly when your code enters the blocked state, giving you the opportunity to carry out some additional processing before blocking. Polling: This method involves repeatedly testing the state of an asynchronous ope

35、ration to determine if its complete. This is a very simple technique and is not particularly efficient from a processing perspective. You should avoid tight loops that consume processor time. Its best to put the polling thread to sleep for a period using Thread.Sleep between completion tests. Becaus

36、e polling involves maintaining a loop, the actions of the waiting thread are limited, but you can easily update some kind of progress indicator. Waiting: This method uses an object derived from the System.Threading.WaitHandle class to signal when the asynchronous method completes. Waiting is amore e

37、fficient version of polling and in addition allows you to wait for multiple asynchronous operations to complete. You can also specify time-out values to allow your waiting thread to fail if the asynchronous operation takes too long, or if you want to periodically update a status indicator. Callback:

38、 This a method that the runtime calls when an asynchronous operation completes. The calling code does not need to take any steps to determine when the asynchronous operation is complete and is free to continue with other processing. Callbacks provide the greatest flexibility, but also introduce the

39、greatest complexity, especially if you have many concurrently active asynchronous operations that all use the same callback. In such cases, you must use appropriate state objects to match completed methods against those you initiated.Caution When using the asynchronous capabilities of the SQL Server

40、 data provider, you must ensure that your code does not inadvertently dispose of objects that are still being used by other threads. Pay particular attention to SqlConnection and SqlCommand objects.创建一个基于Windows的应用程序的命令行问题 您需要使用C 命令行编译器建立一个应用程序,提供了一个Windows窗体为基础的图形用户界面。解决方案 创建一个类,它扩展了System.Windows.

41、Forms.Form类。 (这将是您的应用的主要形式。 )在您的一个类,确保您执行一个静态方法命名为主要。在Main方法中,创建一个实例,您的主窗体类,并将其交给静态方法运行System.Windows.Forms.Application级。建立您的应用程序使用命令行C 编译器,并指定/目标: winexe编译开关。注意:如果您自己的Visual Studio中,您将最经常使用的Windows应用程序项目模板创建新的Windows窗体应用程序。建设大型基于GUI应用是一个非常耗时的工作,涉及到正确的实例,配置和布线的许多形式和控制。大部分的Visual Studio的自动化有关的工作与建设的图

42、形应用。试图建立一个大型的图形应用程序的帮助的工具如Visual Studio将带您更长时间,是极其单调乏味,并导致更多的机会的错误在您的代码。然而,它也是有用的知识的基本要求,以建立一个基于Windows的应用程序使用命令行的情况下,您是以往的工作机器上没有Visual Studio和希望创建一个自动化的快速实用的一些任务或者输入一个用户。如何运作 建立一个应用程序,提供了一个简单的Windows图形用户界面是一个远离世界发展一个正式的基于Windows的应用程序。但是,您必须执行某些任务,无论您是否有书面的Windows相当于世界您好或下一版Microsoft Word中,包括下列内容:对

43、于每一个表格您需要在您的应用程序,创建一个类,它扩展了System.Windows.Forms.Form类。在您的每个班的形式,宣布各成员,代表了控制,将在这形式,如按钮,标签,列表和文本框。这些成员应被宣布为私营或者至少保护的,所以其他程序的内容不能访问他们直接联系。如果您需要揭露的方法或属性这些管制,实施必要的成员您的窗体类,提供间接和控制进入载管制。宣告方法在您的窗体类的事件,将处理所提出的控制载的形式,如点击按钮或按键时,一个TextBox是主动控制。这些方法应该私人或受保护的,并按照标准。 NET的活动模式(描述在食谱13-11 ) 。这是在这些方法(或方法调用这些方法)如果您将界定

44、的大部分应用程序的功能。声明一个构造形式为您的每一个类实例的形式的控制和配置的初始状态(大小,颜色,位置,内容等) 。构造应还电线了适当的事件处理方法,您的类的事件,每个控制。声明一个静态方法命名主通常作为一名应用程序的主要形式一流的。此方法是切入点,为您的应用程序,它可以有相同的签名所提到的那些食谱1-1 。在Main方法中,调用Application.EnableVisualStyles来让XP的主题支持下,创建一个实例应用程序的主要形式,并通过它作为论点静态Application.Run方法。 run方法使您的主要形式可见并启动一个标准的Windows消息循环的当前线程,这会将使用者输入

45、(按键,鼠标点击,等等)到您的申请表的事件。连接到数据库问题您需要打开一个连接到一个数据库。解决方案创建一个连接对象适当类型的数据库,你需要连接。所有连接对象执行System.Data.IDbConnection接口。配置通过设置Connection对象的ConnectionString属性。打开连接的要求Connection对象的Open方法。如何运作 第一步,数据库访问是打开一个连接到数据库。该IDbConnection界面是一个数据库连接,每个数据类库提供一个独特的执行。这里是IDbConnection实施的五个标准数据类库: System.Data.Odbc.OdbcConnectio

46、n System.Data.OleDb.OleDbConnection System.Data.OracleClient.OracleConnection System.Data.SqlServerCe.SqlCeConnection System.Data.SqlClient.SqlConnection 您配置一个连接对象使用一个连接字符串。一个连接字符串是一套分号分隔的名称和值的配对。您可以提供一个连接字符串作为一个构造函数参数或通过设置Connection对象的ConnectionString属性之前开放连接。每个连接级执行,您需要提供不同的信息连接字符串。参阅ConnectionStr

47、ing属性为每个文件执行看到价值,您可以指定。可能的设置包括以下内容:名称的目标数据库服务器资料库名称开放初期连接超时值连接池的行为验证机制时所使用的数据库连接的担保,包括提供一个用户名和密码,如果需要的话。 一旦配置,调用Connection对象的Open方法打开连接数据库。然后,您可以使用Connection对象来执行命令,对数据来源。的属性Connection对象也可让您检索信息的连接状态和设置用于打开连接。当您完成连接,你应该总是要求其关闭的方法免费的基本数据库连接和系统资源。 IDbConnection延伸System.IDisposable ,也就是说,每个连接类实现Dispose方法。处置自动呼吁关闭,使using语句一个非常廉洁和高效率的方法使用连接对象在您的代码。您达到最佳的可扩展性,开放的数据库连接才可能和关闭当您已经完成。这可以确保您不占用数据库连接很长时间,所以你让所有代码的最大机会获得连接。这一点尤其重要如果您使用的是连接池。执行异步数据库操作SQL Server问题 您需要执行查询或命令对一个SQL Server数据库作为背景您的应用程序的任务,同时继续与其他处理。解决方案 使用BeginExecuteNonQuery , BeginExecuteReader ,或BeginExec

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论