Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C # how to connect to SQLServer database

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

C # how to connect SQLServer database, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

C # connecting to database: Connection object

Overview of 1.Connection object

The Connection object is a connection object whose main function is to establish a connection to the physical database. It mainly includes four kinds of object classes that access the database, which can also be called data providers, which are described below.

The SQL Server data provider, located in the System.Data.SqlClient namespace. The ODBC data provider, located in the System.Data.Odbc namespace. The OLEDB data provider, located in the System.Data.OleDb namespace. The Oracle data provider, located in the System.Data.OracleClient namespace.

Description: according to the use of the database, introduce different namespaces, and then connect to the database through the Connection object connection class in the namespace. For example, to connect to a SQL Server database, you must first reference the SQL Server data provider through the using System.Data.SqlClient command before you can call the SqlConnection class in the space to connect to the database.

two。 Connect to the database

Take the SQL Server database as an example, if you want to connect to the SQL Server database, you must use the SqlConnection class under the System.Data.SqlClient namespace. So first you need to reference the namespace through the using System.Data.SqlClient command, and after connecting to the database, open the database by calling the Open method of the SqlConnection object. Determine the connection status of the database by the State property of the SqlConnection object.

Interface:

Code:

Private void btn1_Click (object sender, EventArgs e) {if (txt1.Text = = "") {MessageBox.Show ("Please enter the name of the database to connect to!");} else {try {string connString = "server=.;database=" + txt1.Text.Trim () + "; uid=test;pwd=test;connect timeout=5"; / / * SqlConnection sqlConnection = new SqlConnection (connString); / / * * sqlConnection.Open () / / * * if (sqlConnection.State = = ConnectionState.Open) {lab2.Text = "Database [" + txt1.Text.Trim () + "] is connected and open!" ;} catch {MessageBox.Show ("database connection failed!");}

3. Close the connection

When the operation on the database is finished, the connection with the database should be closed and the occupied resources should be released. You can close the connection to the database by calling the Close method or the Dispose method of the SqlConnection object. The main difference between the two methods is that the Close method is used to close a connection, while the Dispose method not only closes a connection, but also cleans up the resources consumed by the connection. When you close the connection using the Close method, you can call the Open method to open the connection without any errors. If you use the Dispose method to close the connection, you cannot directly open the connection with the Open method. You must reinitialize the connection and open it again.

Interface:

Code:

SqlConnection sqlConnection; / / * / Connect to the database / private void btn1_Click_1 (object sender, EventArgs e) {if (txt1.Text = = "") {MessageBox.Show ("Please enter the database name:");} else {try {string connString = "server=.;database=" + txt1.Text.Trim () + "; uid=test;pwd=test;connect timeout=5"; / / * sqlConnection = new SqlConnection (connString); / / * sqlConnection.Open () / / * if (sqlConnection.State = = ConnectionState.Open) {btn1.Text = "connected successfully";}} catch (Exception ex) {MessageBox.Show (ex.Message); txt1.Text = ";} / use the Close method to close the connection and re-call the Open method to connect to the database / private void btn2_Click (object sender, EventArgs e) {try {string str ="; sqlConnection.Close () / / * if (sqlConnection.State = = ConnectionState.Closed) {str = "database closed successfully\ n";} sqlConnection.Open (); / / * if (sqlConnection.State = = ConnectionState.Open) {str + = "database opened successfully\ n";} rtbox1.Text = str;} catch (Exception ex) {rtbox1.Text = ex.Message }} / close the connection using the Dispose method and re-call the Open method to connect to the database / private void btn3_Click (object sender, EventArgs e) {try {sqlConnection.Dispose (); / / * sqlConnection.Open (); / / * *} catch (Exception ex) {rtbox1.Text = ex.Message;}}

After reading the above, have you mastered how C # connects to SQLServer database? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report