This article is only suitable for beginners. If you are a prawn, this article will waste your time.
First, file-> build-> project. Project type: C #, ASP. . NET application.
Drag a sqldataadapter in the toolbox to the webform window.
Click Next in the Wizard to create a new connection. Under the connection tab, select an available server and an available sqlserver database and click OK.
Then configure the query generator (which is too simple to avoid ). Complete.
In webform, click sqldataadapter1 under the Property bar to generate a dataset. Create a single region and click OK.
Now, the connection to the database is basically done. Let's operate on it.
Drag a datagrid to the webform window. Select the data source in the property generator (we only have one dataset.
Then let's look at the code.
Switch to the code window and add the yellow code:
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Namespace WebApplication1
{
/// <Summary>
/// Summary description for WebForm1.
/// </Summary>
Public class WebForm1: System. Web. UI. Page
{
Protected System. Data. SqlClient. SqlDataAdapter sqlDataAdapter1;
Protected System. Data. SqlClient. SqlCommand sqlSelectCommand1;
Protected System. Data. SqlClient. SqlCommand sqlInsertCommand1;
Protected System. Data. SqlClient. SqlConnection sqlConnection1;
Protected System. Web. UI. WebControls. DataGrid DataGrid1;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Data. SqlClient. SqlDataAdapter sqlDataAdapter2;
Protected System. Data. SqlClient. SqlCommand sqlSelectCommand2;
Protected System. Data. SqlClient. SqlCommand sqlInsertCommand2;
Protected System. Data. SqlClient. SqlCommand sqlUpdateCommand1;
Protected System. Data. SqlClient. SqlCommand sqlDeleteCommand1;
Protected System. Data. SqlClient. SqlConnection sqlConnection2;
Protected System. Web. UI. WebControls. DataGrid DataGrid2;
Protected WebApplication1.DataSet1 dataSet11;
Public WebForm1 ()
{
Page. Init + = new System. EventHandler (Page_Init );
}
Private void Page_Load (object sender, System. EventArgs e)
{
// Put user code to initialize the page here
}
Private void Page_Init (object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web Form Designer.
//
InitializeComponent ();
// Fill the dataset
SqlDataAdapter1.Fill (dataSet11 );
// Bind
DataGrid1.DataBind ();
} # Region Web Form Designer generated code
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
# Endregion
}
Generate a program. If it is normal, you can see your data in the datagrid.
If you have higher requirements on the data to be operated (only if SQL statements are required), you can click the plus sign before the green code to modify the blue code below.
Private void InitializeComponent ()
{
This. sqlConnection1 = new System. Data. SqlClient. SqlConnection ();
This. dataSet11 = new WebApplication1.DataSet1 ();
This. sqlInsertCommand1 = new System. Data. SqlClient. SqlCommand ();
This. sqlSelectCommand1 = new System. Data. SqlClient. SqlCommand ();
This. sqlDataAdapter1 = new System. Data. SqlClient. SqlDataAdapter ();
This. sqlDataAdapter2 = new System. Data. SqlClient. SqlDataAdapter ();
This. sqlSelectCommand2 = new System. Data. SqlClient. SqlCommand ();
This. sqlInsertCommand2 = new System. Data. SqlClient. SqlCommand ();
This. sqlUpdateCommand1 = new System. Data. SqlClient. SqlCommand ();
This. sqlDeleteCommand1 = new System. Data. SqlClient. SqlCommand ();
This. sqlConnection2 = new System. Data. SqlClient. SqlConnection ();
(System. ComponentModel. ISupportInitialize) (this. dataSet11). BeginInit ();
This. Button1.Click + = new System. EventHandler (this. button#click );
//
// SqlConnection1
//
This. sqlConnection1.ConnectionString = "data source = (local); initial catalog = xr; integrated security = SSPI; persist security" +
"Info = False; workstation id = XURUI; packet size = 4096 ";
//
// DataSet11
//
This. dataSet11.DataSetName = "DataSet1 ";
This. dataSet11.Locale = new System. Globalization. CultureInfo ("zh-CN ");
This. dataSet11.Namespace ="Http://www.tempuri.org/DataSet1.xsd";
//
// SqlInsertCommand1
//
This. sqlInsertCommand1.CommandText = "insert into store (zipcode, area, name) VALUES (@ zipcode, @ area, @ name); SELECT zi" +
"Pcode, area, name FROM store ";
This. sqlInsertCommand1.Connection = this. sqlConnection1;
This. sqlInsertCommand1.Parameters. add (new System. data. sqlClient. sqlParameter ("@ zipcode", System. data. sqlDbType. char, 10, System. data. parameterDirection. input, true, (System. byte) (0), (System. byte) (0), "zipcode", System. data. dataRowVersion. current, null ));
This. sqlInsertCommand1.Parameters. add (new System. data. sqlClient. sqlParameter ("@ area", System. data. sqlDbType. char, 10, System. data. parameterDirection. input, true, (System. byte) (0), (System. byte) (0), "area", System. data. dataRowVersion. current, null ));
This. sqlInsertCommand1.Parameters. add (new System. data. sqlClient. sqlParameter ("@ name", System. data. sqlDbType. char, 10, System. data. parameterDirection. input, true, (System. byte) (0), (System. byte) (0), "name", System. data. dataRowVersion. current, null ));
//
// SqlSelectCommand1
//
This. sqlSelectCommand1.CommandText = "SELECT zipcode, area, name FROM store ";
This. sqlSelectCommand1.Connection = this. sqlConnection1;
//
// SqlDataAdapter1
//
This. sqlDataAdapter1.InsertCommand = this. sqlInsertCommand1;
This. sqlDataAdapter1.SelectCommand = this. sqlSelectCommand1;
This. sqlDataAdapter1.TableMappings. AddRange (new System. Data. Common. DataTableMapping [] {
New System. data. common. dataTableMapping ("Table", "store", new System. data. common. dataColumnMapping [] {...
...........................
You can generate a dataset based on your needs.
You can add a new column as follows:
DataTable dt; // define a table (nonsense)
Dt = dataSet11.Tables ["your table"]; // pay the table Value
DataRow dr;
Dr = d