Intimate contact asp.net (8) DataSet

Source: Internet
Author: User
Tags mdb database

Let's talk about DataSet here, this is a powerful thing, we have a preliminary understanding of it in the last section, here we want to talk about it if used to manipulate the data. is also very simple, but powerful, to speak the whole, is unlikely, because the dataset and the DataGrid control account for more than 50% of the. Net help, and this total has 23M ...
Here we set up a Test.mdb database, as a test database, which has a table named ASPCN, the data structure as shown:

Figure 8-1

A database operation without a dataset

Before we use the dataset, let's take a look at how the dataset is inserted, updated, and deleted by the asp.net without using datasets.

To insert the data, update, delete and can not use the dataset, there is no way, only with SQL directly come: I will only talk about how to insert, because the other operation is only to change the SQL statement, I think we have no problem. 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)
{

file:// Data submitted by households

}
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+ ')";
file:// to start a 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.

We submit the data to the database, from the user input, here is a we often see the <form> form, the junior to see and ordinary no difference, but we take a closer look at its <form> properties, runat= "Server", Oh, is such a small label, the nature of the program has changed. This shows that he is a Web form. I use HTML controls here to show you what the difference is between ASP.net and ASP.

<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 above code, there is one thing to note is that the Trigger button control submission Event processing is OnServerClick, where we have little attention, because you use the Web control has become a habit, it is easy to use the onclick event, hehe, this error is difficult to find, Wrong do not know how wrong:)

Below we should handle the commit event in the Submit_click, first is gathers the information, here we also saw the Long-lost request object and the 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 and to open and execute the database, as we have said in the previous section. 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+ ')" ;

We also need to pay attention to my connection statement (because many people ask me how to link the MDB database, in fact, and the ASP is the same, 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 write so much, had to stay to the next section to speak DataSet.



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.