Visual C # SQL Server programming

zhaozj2021-02-16  86

Visual Studio.NET's Chinese Beta 2 has been launched a period of time, compared to early Beta 1 versions, new versions have great changes, including SQL Server programming, in Chinese Beta 2, database The access is generally modified by OLE DB. Since database programming is the core of enterprise-level application development, this article will illustrate the SQL Server programming method in the BETE 2 version.

default setting

First, we need to install SQL Server 2000 in the application system, and the SQL OLE DB will also be installed automatically, then create a database called Tyjdb with SQL Server Enterprise Manager, and build a new one For address's data sheets, it contains four fields of Name, Email, AGE, and Address. Open the server resource manager in the view menu item in the VS development environment, which manages SQL Server and other OLE DB database connections and manage it. Then we add a new data connection, the connection property selects Microsoft Ole DB Provider for SQL Server, then select the server and database Tyjdb, and then press OK after the test connection is successful. Use this tool to generate a needed database connection string.

Connect to the database

Create a new ASP.NET project or Windows.net application because the data inventory of both is the same. Drag the data connections in the Server Explorer to the newly created Web Form, then automatically generate a connection string as follows:

this.sqlConnection1.ConnectionString = "data source = WHOAMI; initial catalog = tyjdb; integrated security = SSPI; persist security info = False; workstation id = WHOAMI; packet size = 4096"; wherein WHOAMI server name of the author.

Select the SQLDATAADAPTER in the toolbox and drag to the web form, select the Tyjdb data connection, select Use the SQL statement to access the database, generate the SQL statement, only fill in SelectFrom Address, confirm that it is completed. The program generated code is as follows:

protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; // access main classes of databases protected System.Data.SqlClient.SqlCommand sqlSelectCommand1; // SQL statement type protected System.Data.SqlClient.SqlConnection sqlConnection1 treated; // database connection class InitializeComponent () has the following declaration: this.sqlConnection1 = new System.Data.SqlClient.SqlConnection (); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter (); this.sqlSelectCommand1 = new System.Data.SqlClient .SqlCommand (); this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlSelectCommand1.CommandText = "SELECT name, email, age, address FROM address"; this.sqlSelectCommand1.Connection = this.sqlConnection1; such that the data can Table Displayed in the web form, join a DataGrid control to the Web Form and add the following statement in Page_init:

SqlConnection1.Open (); // Open database connection DataSet objDataSet; // New DataSetObjDataSet = new dataset (); sqldataadapter1.fill (ObjDataSet, "Address"); // Fill data into datasetdataGrid1.DataSource = Objdata .Tables ["address"]. DefaultView; // Related Dataset and DataGridDataGrid1.DATABIND (); // Binding data SQLConnection1.close (); // Turn off Database connection

After compiling execution, Web Form can display data in the database in the DataGrid.

Increasing data

When you want to add database data, we only need to add textbox for the number of numbers on the web form, and add a button, then add the Click event for the button, the specific code is as follows:

. SqlInsertCommand1.Parameters [ "@ name"] Value = TextBox1.Text; // assign to the corresponding parameter TextBox sqlInsertCommand1.Parameters [ "@ email"] Value = TextBox2.Text;. SqlInsertCommand1.Parameters [ "@ age"]. Value = TextBox3.Text; sqlInsertCommand1.Parameters [ "@ address"] Value = TextBox4.Text;. sqlInsertCommand1.Connection.Open (); // open connection sqlInsertCommand1.ExecuteNonQuery (); // Insert statement execution sqlInsertCommand1.Connection.Close (); // Close connection sqlconnection1.open (); DataSet objDataSet; // The following block updates DataGridobjDataSet = new dataset (); sqldataadapter1.fill (objDataSet, "address"); dataGrid1.datasource = objDataSet.Tables ["Address "] .DEFAULTVIEW; DATAGRID1.DATABIND (); When performing this program, simply fill the value of the record field you want to add in TextBox, then press the button to perform the add function.

delete data

When you want to delete database data, we need to add a TextBox5 and a button on the web form, add the following code for the button:

SqlCommand sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand (); // declare Sql Command class object this.sqlDataAdapter1.DeleteCommand = sqlDeleteCommand1; sqlDeleteCommand1.CommandText = "DELETE FROM ADDRESS WHERE NAME = '" TextBox5.Text "'"; // SQL statement sqldeleteCommand1.connection = this.sqlConnection1; // Data connection SQLDeleteCommand1.connection.Open (); sqlDeleteCommand1.executenonQuery (); // Execute the SQL statement sqldeleteCommand1.Connection.Close ();

When performing this program, simply fill the value of the record name field you want to delete in TextBox 5, then press the button to perform the delete function. Update the principle of operation, there are many techniques to enrich the above programs in the specific development, such as increasing error handling, etc., which is limited to the unity of the space. In summary, you can make full use of the development environment of Visual Studio.NET to simplify programming to improve programming efficiency and quality. The above procedures are developed with C #, in Chinese Windows 2000 Server, SQL Server 2000, Visual Studio.Net Chinese Beta 2 Environment, compiled and running normally, Chinese shows normal.

转载请注明原文地址:https://www.9cbs.com/read-13655.html

New Post(0)