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 ";
Where whoami is the author's server name.
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 SelectFrom Address, confirm that it is completed. The program generated code is as follows:
Protected system.data.sqlclient.sqldataadapter SqlDataAdapter1;
// Access the main class of the database
protected system.data.sqlclient.sqlcommand sqlselectcommand1;
// SQL statement processing class
protected system.data.sqlclient.sqlconnection sqlconnection1;
/ / Connect the class of the database
There is a statement in InitializationComponent ():
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;
THISQLSELECTCOMMAND1.COMMANDTEXT = "SELECT NAME, Email, Age, Address from address"; this.sqlselectcommand1.connection = this.sqlConnection1;
To make the data in the Web Form, add a DataGrid control to the web form and add the following statement in Page_init:
SqlConnection1.open ();
// Open the database connection
DataSet objDataSet;
// Newly build a data-based DataSet
Objdataset = new dataset ();
SqlDataAdapter1.fill (ObjDataSet, "Address");
// Fill data in DataSet
DataGrid1.datasource = objDataSet.tables ["address"]. Defaultview;
// Related DataSet and DataGrid
DataGrid1.databind ();
// Binding data
SqlConnection1.close ();
/ / Turn off the 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 the TEXTBOX to the corresponding parameters
SqlinsertCommand1.Parameters ["@ email"]. value = textbox2.text;
SqlinsertCommand1.Parameters ["@ agn"]. value = textbox3.text;
SqlinsertCommand1.Parameters ["@ address"]. value = textbox4.text;
SqlinsertCommand1.connection.open ();
// Open connection
SqlinsertCommand1.executenonQuery ();
/ / Execute an Insert statement
SqlinsertCommand1.connection.close ();
// Close connection
SqlConnection1.open ();
DataSet objDataSet;
// Update DataGrid below
Objdataset = 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 to use
SqlDeleteCommand1.connection.Open ();
SqlDeleteCommand1.executenonQuery ();
/ / Execute this 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.