Contact ASP.Net(8 intimately)
Tuesday, March 03, 2009 by rain
Still be told here tell DataSet,This is a function is powerful east east,We had an abecedarian to understand to it in on one,If use operand to occupy,we should tell our here.Also be very simple east east,But the function is powerful, should tell complete,Unlikely,Because DataSet and DataGrid accuse the 50% above that explained to occupy.Net to help,And this total mutual 23M. . .
We build database of a Test.mdb here,As the database of the test,Have a list that the name is Aspcn among them,If data structure pursues
Graph 8-1
Do not require the operation counting a library of DataSet
Before the application that says DataSet,Look not to use DataSet how to undertake insertion to the database in ASP.net in light of us,Update,Delete.
Should undertake insertion to data,Update,Delete and cannot use DataSet,Without method,Only useful SQL comes directly:) ,Tell how to be inserted only here,Because the operation of other just corrects SQL statement,I think everybody does not have a problem.Because here is used is Mdb database,So be about this to was operated with ADO.net.
We see whole source code say again first
%26lt;% @ Page Language= %26quot;C#%26quot; %%26gt;
%26lt;% @ Import Namespace= %26quot;System.Data%26quot; %%26gt;
%26lt;% @ Import Namespace= %26quot;System.Data.ADO%26quot; %%26gt;
%26lt;Script Language= %26quot;C#%26quot; Runat= %26quot;Server%26quot; %26gt;
Public String MyConnstring= "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;" ;
ADOConnection MyConnection;
Public Void Page_Load(Object Src, eventArgs E)
{
/ / the user submits data
}
Public Void Submit_Click(Object Src, eventArgs E)
{
String Username1 = Request.Form["username" ];
String Address1 = Request.Form["address" ];
String School1 = Request.Form["shool" ];
String StrInsert= "insert Into Aspcn(username, address, school)
Values(' "+username1+" ' , ' "+address1+" ' , ' "+school1+" ') ";
/ / begin coupling
Try
{
MyConnection = New ADOConnection(myConnstring);
ADOCommand MyCommand = New ADOCommand(strInsert, myConnection);
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
Info.Text= " data has saved " ;
}
Catch(Exception Ee)
{
Info.Text= " happening is wrong:" + Ee.Message;
}
}
%26lt;/script%26gt;
%26lt;html%26gt;
%26lt;head%26gt;
%26lt;title%26gt;%26lt;/title%26gt;
%26lt;/head%26gt;
%26lt;body%26gt;
%26lt;asp:LAbel Id= %26quot;Info%26quot; Runat=server /%26gt;%26lt;br%26gt;
%26lt;form Runat= %26quot;server%26quot; %26gt;
User name: %26Lt;input Type= "text" Name= "username " Size= %26quot;20%26quot; %26gt;%26lt;br%26gt;
Address: %26Lt;input Type= "text" Name= "address " Size= %26quot;20%26quot; %26gt;%26lt;br%26gt;
The school:%26lt;input Type= %26quot;text%26quot; Name= %26quot;school%26quot; Size= %26quot;20%26quot; %26gt;%26lt;br%26gt;
%26lt;input Type= %26quot;button%26quot; Value= %26quot; refers %26quot; Runat= %26quot;server%26quot; OnServerClick= %26quot;submit_Click%26quot; %26gt;
%26lt;p%26gt;1%26lt;/p%26gt;
%26lt;/form%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
The program above carries out a circumstance to look below two graphs:
Before graph 8-2 is carried out
After graph 8-3 is carried out
Because we are used Access, be about to declare above all so
%26lt;% @ Import Namespace= %26quot;System.Data%26quot; %%26gt;
%26lt;% @ Import Namespace= %26quot;System.Data.ADO%26quot; %%26gt;
Making clear what use here is ADO.net.
The data that we submit a database,Come from at the input of the user,What use here is a %26lt;form%26gt; watch sheet that we often see,What don't look first and have commonly to distinguish,But what is the attribute in the %26lt;form%26gt; that we see it carefully, runat= "server" , breathe out ah,Namely so a little label,The property of this program changes.This showed he is a Web Form.I used HTML here accuse,The purpose has what distinction after all to let everybody see ASP.net and ASP namely.
%26lt;form Runat= %26quot;server%26quot; %26gt;
User name: %26Lt;input Type= %26quot;text%26quot; Name= %26quot;username%26quot; %26gt;%26lt;br%26gt;
Address: %26Lt;input Type= %26quot;text%26quot; Name= %26quot;address%26quot; %26gt;%26lt;br%26gt;
The school:%26lt;input Type= %26quot;text%26quot; Name= %26quot;school%26quot; %26gt;%26lt;br%26gt;
%26lt;input Type= %26quot;button%26quot; Value= %26quot; refers %26quot; Runat= %26quot;server%26quot; OnServerClick= %26quot;submit_Click%26quot; %26gt;
%26lt;/form%26gt;
Above code,A bit of need notices is,Spark Button accuses those who refer incident processing is OnServerClick, here everybody has an attention rarely,Because everybody uses WEB to accuse already became a custom,Used OnClick incident very easily,Breathe out ah,This kind of mistake discovers very hard,wrong do not know how wrong:)
We are with respect to this below in Submit_Click processing refers incident,It is to gather information above all,We saw long-unseen Request object and Form method again here.This is mixed ASP is almost exactly like,I also did not want to say more.
String Username1 = Request.Form["username" ];
String Address1 = Request.Form["address" ];
String School1 = Request.Form["shool" ];
Will collect the data that come to write a database namely next,About open and be being carried out of the database,On us one also had been told.Here also not long-winded.The validity that notes SQL statement goes.
String StrInsert= "insert Into Aspcn(username, address, school) Values('" +username1+ "' , '" +address1+ "' , '" +school1+ "')" ;
The coupling statement that everybody also wants to notice me (because a lot of people will ask me,How to connect Mdb database, mix actually Asp is same,Basically be everybody has some of person to not be willing to already started work oneself most go carrying out,Ask the person had made a habit)
Public String MyConnstring= "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;" ;
The statement with here coupling the most economic Access (the individual thinks) ,Actually still a lot of kinds write a law.For instance:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/test/test.mdb
Data Source=aspcn
Breathe out ah,Originally this one is to want those who tell DataSet,The character that did not think of other wrote so much at a draught,Be forced to stay to below one to tell DataSet