Intimate contact asp.net (8)

Source: Internet
Author: User
Tags commit mdb database
ASP.net Our previous chapters describe how to display information in a database and the basic usage of DataReader and datasets.
Have a friend say not enjoyable, want me to write the following chapter quickly, very sorry, I really do not have much time, write this
Some are very slow. (Don't throw eggs, at most I don't talk nonsense)

Let's talk about DataSet here, this is a powerful stuff, we have a in the last section
Preliminary understanding, here we want to talk about it if used to manipulate the data. is also very simple dongdong, but the function is formidable, must
It's not possible, because the dataset and DataGrid controls account for more than 50% of. Net Help, and this
A total of 23M ...

Here we set up a Test.mdb database as a test database, with a table named ASPCN, with a number
According to the structure as shown


Figure 8-1

A database operation without a dataset

Before we use the dataset, let's take a look at how to insert a database without using a dataset in asp.net.
Update, delete.

To insert, UPDATE, delete data without using a dataset, there is no way, only SQL directly come:
, I'll just talk about how to insert, because the other operation is only to change the SQL statement, I think we have no problem
it. Because of the use of the MDB database, this will be done with ado.net.

Let's look at the whole source code first.


<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.ADO"%>
<script language= "C #" runat= "Server" >
public string myconnstring= "Driver={microsoft Access Driver (*.mdb)};
Dbq=c:/test/test.mdb; ";
ADOConnection myconnection;
public void Page_Load (Object Src,eventargs E)
{

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+ ')";
Start the connection
Try
{
myconnection = new ADOConnection (myConnString);
Adocommand mycommand = new Adocommand (strinsert,myconnection);
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
info.text= "Data has been saved";
}
catch (Exception ee)
{
Info.text= "error occurred:" +ee. message;
}

}
</script>
<title></title>
<body>
<asp:label id= "Info" Runat=server/><br>
<form runat= "Server" >
User name: <input type= "text" name= "username" size= "><br>"
Address: <input type= "text" name= "adress" size= "><br>"
School: <input type= "text" Name= "school" size= "><br>"
<input type= "button" value= "Submit" runat= "Server"
onserverclick= "Submit_click" >
<p>1</p>
</form>
</body>


The above program performance looks at the following two diagrams:


Figure 8-2 prior to implementation

Figure 8-3 After execution


Because we are using access, so first of all, we must declare

<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.ADO"%>

Indicates that we are using ado.net here.

The data we submit to the database comes from the user input, which uses a <form> that we often see
The form, the junior look and normal is no different, but we carefully look at its <form> in the properties of what, RU
nat= "Server", hehe, is such a small label, the nature of this program has changed. This shows that he is a
Web Form. Here I used HTML controls to show you how asp.net and ASP
Some difference.

<form runat= "Server" >
User name: <input type= "text" name= "username" ><br>
Address: <input type= "text" name= "adress" ><br>
School: <input type= "text" Name= "School" ><br>
<input type= "button" value= "Submit" runat= "Server"
onserverclick= "Submit_click" >
</form>

In the code above, one thing to note is that the Trigger button control commit event is handled by OnServerClick
, there is little attention here, because everyone uses the Web control has become a habit, it is easy to use the onclick event, OH
Ah, this kind of mistake is hard to find, wrong do not know how wrong:

Here we should deal with the commit event in Submit_click, the first is to collect the information, here we see the
A Long-lost Request object and form method. This is almost identical with the ASP, I do not want to say more.

String username1 = request.form["username"];
String Address1 = request.form["Address"];
String school1 = request.form["Shool"];

The next step is to write the collected data to the database, on the opening and execution of the database, as we said in the previous section
Out. There's no nagging here. Note that the correctness of the SQL statement is OK.

String strinsert= "INSERT into ASPCN (Username,address,school)
VALUES (' +username1+ ', ' +address1+ ', ' "+school1+ ')";

You should also pay attention to my join statement (because many people ask me how to link the MDB database, in fact, and ASP is
, the most important is that some people do not want to do their own practice, asking people has become a habit.

public string myconnstring= "Driver={microsoft Access Driver (*.mdb)};
Dbq=c:/test/test.mdb; ";

This is the most useful statement of access (personally), but there are many ways to do it. Like what:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/test/test.mdb

Data SOURCE=ASPCN

Oh, originally this section is to speak DataSet, did not think of other text suddenly wrote so much, had to stay to
We'll talk about the dataset in the next section.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.