[C #] ASP.NET and database links

[C #] ASP.NET and database links


Usually when writing web pages, will need to connect to the database and retrieve data from or queries than on the other,

Next is to show how the ASP.NET be linked to the database.

First, the database connection, you must first think and keep up the page description which database link,

And database user accounts, passwords, location and other information must be stated,

Most sites will typically introduced when setting up a database link from a web page xxxxx.aspx.cs how to link in,

But because of his often encountered in the development of one thing, that is, the user name of the database, password changes,

Although generally less likely to go ... but sometimes change is not the time to develop their own web pages, it will always happen,

And many, when creating the database link in xxxxx.aspx.cs in, because the page must be linked,

So often it leads to a change is necessary to change the dilemma of many pages , so I recommend Web.config establishment of a database link syntax,

The advantage is that, if you need to modify user accounts, passwords, or even just need to modify a database name,

In addition, when there is a greater development to the site, usually because of user access rights to different pages vary,

And this method can also manage all links grammar in the same place.

The next direct description of the syntax, but also here along with instructions when using a difference when MySQL and MSSQL,

Web.config is connected to the first setting method,

File name: Web.config


	
  
  
	
  
   

   
  

Above the name field, respectively MSSQL_DBconnect and MySQL_DBconnect two names,

Respectively illustrate syntax MSSQL and MySQL is coupled to the other must import the MySQL dll file itself using MySQL ,

In addition, the syntax 127.0.0.1 modify the database location (normal beginners same database and development environment),

DB_Username, DB_Password, DB_Name please make corrections in accordance with their own settings.

Next, it is to explain how to apply Web.config links with France in xxxxx.aspx.cs.

Where you want to link to the database is set to increase grammar,


using System.Data.SqlClient;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL_DBconnect"].ConnectionString);
//MySQL连结:
using MySql.Data.MySqlClient;
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL_DBconnect"].ConnectionString);

MySQL_DBconnect which corresponds to the Web.config and MSSQL_DBconnect is in the name of the new.

This allows you to connect to the database ASP.NET.

Next, a simple example to read a packet of data in the database,


SqlDataReader reader;	//声明一个DataReader
SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL_connect"].ConnectionString);
String select = "select * from USER where Name = God";	//声明SQL语法的字符串,这边可依照自行需求修改
connStr.Open();	//开启数据库的连结
SqlCommand cmd = new SqlCommand(select, connStr);	//声明SqlCommand并将SQL语法及连结语法带入
reader = cmd.ExecuteReader();	//使用SqlCommand的ExecuteReader()方法,
				//ExecuteReader()为查询时使用,如要删除、修改、新增,须改为ExecuteNonQuery()方法
while (reader.Read())	//使用无限循环将SQL语法查询的结果每笔查阅一次
{
	String NameStr = (String)reader["Name"];//将数据库中Name字段存放于NameStr字符串中
	int IDint = (int)reader["ID"];		//将数据库中ID字段存放于IDint整数中,不同型态请自行更改
					//这边建议字段与字符串相同,方便管理,怕人搞混所以这边用不同名称
}
connStr.Close();	//关闭数据库的连结


//MySQL语法:说明与上面MSSQL说明相同,但通常Sql的字眼会变成MySql
MySqlDataReader reader;
MySqlConnection connStr = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQL_connect"].ConnectionString);
String select = "select * from USER where Name = God";
connStr.Open();
MySqlCommand cmd = new MySqlCommand(select, connStr);
reader = cmd.ExecuteReader();
while (reader.Read())
{
	String Name = (String)reader["Name"];
	int IDint = (int)reader["ID"];
}
connStr.Close();

The example above, I would string and integer statement within a loop, so the loop can be used only within this string and integer,

So please have different needs make changes.

As the example SQL syntax has been screened for a particular Name, if there are different needs, can be used in the inner loop is recommended to determine if screening

※ If more typo or error, please inform, thank you.

Original: Large column  [C #] ASP.NET and database links


Guess you like

Origin www.cnblogs.com/chinatrump/p/11505138.html