Asp. NET connection Database code (ACCESS,MSSQL)

Source: Internet
Author: User
Tags database join httpcontext mssql access database

Specific methods

asp.net connection access code

The code is as follows Copy Code


<%@ Page language= "C #"%>
<%@ import namespace= "System.Data"%>
<%@ import namespace= "System.Data.OleDb"%>
<script runat= "Server" >
Insert page code here
//
void Page_Load () {
String db=@ "Database/data.mdb";
String connstr= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +server.mappath (db) + ";";
String sqlcmd= "CREATE TABLE Ieb_webs (ID identity primary key,title varchar (255) Default Null)";
OleDbConnection conn=new OleDbConnection (CONNSTR);
Conn. Open ();
OleDbCommand olecmd=new OleDbCommand (sqlcmd,conn);
Olecmd.executenonquery (); Execute SQL command
Response.Write ("Data table build!") ");
}
Conn. Close ();
Conn=null;
Olecmd=null;
}
</script>
<body>
<form method= "POST" runat= "Server" >
<!--Insert content here-->
</form>
</body>

Failed to connect to access reason analysis

Failed to process AccessDataSource connection

(1) In the Ch02 folder, create a file named Tio-4-connectionfailure-cs.aspx. In Design view, add a AccessDataSource control to the page that points to Northwind, and the control selects all columns from the table.

(2) Add the GridView to display the information in the data source control. Also, add a Label control and name it "message."

(3) Now switch to Source view and make some changes to the markup, as shown in the following highlighting code. If there is a tag part, it is deleted. The code left should look like the following:

The code is as follows Copy Code



Chapter 2 TIO #4 Connection failure to Access in C #


Chapter 2 TIO #4 Connection failure to Access in C #





Datasourceid= "Accessdatasourcel"
Autogeneratecolumns= "true"
emptydatatext= "No data records were returned"/>
Selectcommand= "Select * FROM Products"
Datafile= "~/app_data/northwind.mdb"
Onselected= "Accessdatasourcel_selected"
/>


  

(4) Check the page; When viewing products sold by Northwind, there should be no problem.

(5) The code will now be added to handle the connection problem. Go to the top of the page and go to the following script. The first example is written in C #, and the second example is written in VB. into one of them.

  code is as follows copy code


<%@ Page language= "C #"%>

Database path Issues

Configure the Access database driver and database file name in Web.config.
Please look at the code

The code is as follows Copy Code
<appSettings>
<add key= "Dbdriver" value= "provider=microsoft.jet.oledb.4.0; Data Source = "/>
<add key= "dbname" value= "Company.mdb"/>
</appSettings>

Get the Access database link string in the database access layer, such as OleDBHelper.cs.

The code is as follows Copy Code

/**////<summary>
To obtain a database join string from web.config
</summary>
To get the database name from the configuration file
public static readonly String dbname = ConfigurationManager.AppSettings.Get ("dbname"). ToString ();
Get database driver from configuration file
public static readonly String dbdriver = ConfigurationManager.AppSettings.Get ("Dbdriver"). ToString ();
Get the database connection string
private static string dbconnectionstring = Dbdriver + HttpContext.Current.Server.MapPath ( HttpContext.Current.Request.ApplicationPath + "/app_data/") + dbname;
To establish a database connection object
private static OleDbConnection oledbconn = new OleDbConnection (dbconnectionstring);
After this setting, the database can be accessed correctly by the above code, regardless of any subdirectories.

asp.net connection MSSQL Database

. Net connection to the MS SQL database

The code is as follows Copy Code

String connectionstring= "Workstation id=" +hostname+ ";p acket size=4096;user id=" +username+ ";p assword=" +password+ "; Data source= "+hostname+";p ersist security info=false;initial catalog= "+databasename;

SqlConnection Connection = new System.Data.SqlClient.SqlConnection ();

connection.connectionstring=connectionstring;

This. Connection.Open ();

System.Data.SqlClient.SqlCommand SelectCommand;

selectcommand= new System.Data.SqlClient.SqlCommand ();

SelectCommand.CommandText = "SELECT * from" +TABLENAME;

SelectCommand.Connection = this. Connection;

System.Data.SqlClient.SqlDataReader dr= Selectcommand.executereader ();


or write like that.

  code is as follows copy code

SqlConnection sql = new SqlConnection (@ "server=.sql2005;integrated Security=sspi; Persist Security info=false;initial catalog=test;data source=7085360cb900427 ");
   
  Try
  {
  SQL. Open ();
  if (SQL. state = = ConnectionState.Open
  Label1. Text = "Connection succeeded!" ";
 }
  catch (SqlException S)
  {
  MessageBox.Show (s.message);

 }
  finally
  {
  SQL. Close ();
  SQL. Dispose ();
 }

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.