Attack on ADO (a)

Source: Internet
Author: User

SQL Server 2008 Overview and Connectivity

June 10, 2014 09:17:13

Ado. NET overview and some common classes refer to "C # Advanced Programming"; Here you can download: http://pan.baidu.com/s/1gdHx979

SQL Server2008 Connection (i) using a database connection

In order to access the database, you need to provide some type of connection parameter, such as the computer running the database and the logon certificate. Users who use ADO will soon become familiar with the. NET connection classes OleDbConnection and SqlConnection. The sample in this article uses the Northwind database, which can be found on the network. The following code snippet shows how to create, open, and close a connection to the Northwind database.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 using System.Data.SqlClient;public static SqlConnection MyConnection()         {             //以下两个连接字符串都可以使用             //string source = "server = localhost;User Id = sa; Password = sa; database = BookTrainTicket";             string source = "server = localhost;Integrated security = SSPI; database = BookTrainTicket";//推荐            SqlConnection myconn = new SqlConnection(source);             try             {                 myconn.Open();                            }             catch             {                 MessageBox.Show("数据库连接失败!!请联系管理员!!");                 System.Environment.Exit(1);             }             return myconn;         }
(ii) Use of configuration files

In the previous. NET version, the connection string for the database is managed by the developer, often by storing the connection string in the application configuration file, or, more commonly, by hard-coding the connection string somewhere in the application. Starting with. NET2.0, there is a predefined way to store connection strings, even using database connections in ways that are unknown in type. For example, you can now write applications and then insert individual database providers without modifying the main application. To define a database connection string, use the <connectionStrings> section in the configuration file. Here you can specify the name of the connection, the actual parameters of the database connection string, and the provider that specifies the connection type. Here is an example:

Configuration file:

<?xml version="1.0"encoding="Utf-8"?><configuration> <startup> <supportedruntime version="v4.0"sku=". netframework,version=v4.5"/> </startup> <appSettings> <add key="TargetUrl"Value="server= (local); Integrated security = Sspi;database = Booktrainticket"/> </appSettings> <connectionStrings> <add name="Booktrainticket"ProviderName="System.Data.SqlClient"connectionString="server= (local); Integrated security = Sspi;database = Booktrainticket"/> </connectionStrings></configuration>

To read data from a configuration file:

1  Private voidBtnconnection_click (Objectsender, EventArgs e)2         {3             //string connstr = system.configuration.configurationmanager.appsettings["TargetUrl"];4             stringConnStr = system.configuration.configurationmanager.connectionstrings["Booktrainticket"]. Connectionstring.tostring ();5SqlConnection myconn =NewSqlConnection (CONNSTR);6             Try7             {8 myconn. Open ();9MessageBox.Show ("Success");Ten             } One             Catch A             { -MessageBox.Show ("database connection Failed!! Please contact the Administrator!! "); -System.Environment.Exit (1); the             }             -}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.