Detailed description of the connection database configuration method in ASP.

Source: Internet
Author: User
Tags connectionstrings
This article mainly introduces the method of configuration of various connection databases in ASP, introduces the MSSQL, Access, Oracle, SQLite, MySQL database configuration, has certain reference value, has the interest to understand.

One, database connection statement

1. MSSQL Database Link Example


<connectionStrings> <add name= "Conn" connectionstring= "server=.; database=demo;uid=sa;pwd=123456 "providername=" System.Data.SqlClient "/> </connectionStrings>

2. Example of Access 2003 database link: "{0}" represents the root directory


<connectionStrings> <add name= "Conn" connectionstring= "provider=microsoft.jet.oledb.4.0; Data source={0}app_data/demo.mdb "providername=" System.Data.OleDb "/> </connectionStrings>

Links to Access 2007 or later


<connectionStrings> <add name= "Conn" connectionstring= "provider=microsoft.jet.oledb.4.0; Data source={0}app_data/demo.mdb "providername=" System.Data.OleDb "/> </connectionStrings>

3. Examples of Oracle database links


<connectionStrings> <add name= "Conn" connectionstring= "Provider=msdaora;data Source=demo; User Id=sa; password=123456, "providername=" System.Data.OracleClient "/> </connectionStrings>

4. SQLite Database Link Example


<connectionStrings> <add name= "Conn" connectionstring= "Data source={0}app_data/demo.db;failifmissing= False "Providername=" System.Data.SQLite "/> </connectionStrings>

5:mysql Database Link Example


<connectionStrings> <add name= "Conn" connectionstring= "host=127.0.0.1; port=3306;database=mysql;uid=sa;pwd=12346 "providername=" MySql.Data.MySqlClient "/> </connectionStrings>

Second, JSON data conversion


Using system;using system.collections.generic;using system.data;using system.linq;using System.Reflection;using System.text;using system.web;using system.web.script.serialization;namespace role.dal{public class JSON {public JSON ( {}///<summary>///Convert DataTable data to JSON data, string concatenation written for///</summary>//<param name= "Jsonname" >jso N name. I didn't find any effect. </param>//<param name= "DT" > obtained data sheet </param>//<returns></returns> public   String Datatabletojson (String jsonname, DataTable dt) {StringBuilder Json = new StringBuilder ();   Json.append ("["); if (dt. Rows.Count > 0) {for (int i = 0; i < dt. Rows.Count;     i++) {json.append ("{"); for (int j = 0; j < dt. Columns.count; J + +) {json.append (dt. COLUMNS[J]. Columnname.tostring () + ": \" "+ dt. ROWS[I][J].      ToString () + "\" "); if (J < dt.      columns.count-1) {json.append (",");     }} json.append ("}"); if (i < dt.    ROWS.COUNT-1) {  Json.append (",");   }}} json.append ("]");  return json.tostring (); }///<summary>///list data conversion to JSON data; string concatenation written, too difficult///</summary>//<typeparam name= "T" ></typeparam&  Gt <param name= "Jsonname" >json name. I didn't find any effect. </param>//<param name= "IL" ></param>//<returns></returns> public string Obje   Cttojson<t> (String jsonname, ilist<t> IL) {StringBuilder Json = new StringBuilder ();   Json.append ("["); if (IL. Count > 0) {for (int i = 0; i < IL. Count;     i++) {T obj = activator.createinstance<t> (); Type type = obj.     GetType (); propertyinfo[] PiS = type.     GetProperties ();     Json.append ("{"); for (int j = 0; J < PiS. Length; J + +) {json.append (pis[j]. Name.tostring () + ": \" "+ pis[j".      GetValue (Il[i], null) + "\" "); if (J < PiS.      Length-1) {json.append (",");     }} json.append ("}"); if (I < IL. Count-1) {Json.append(",");   }}} json.append ("]");  return json.tostring (); }///<summary>//Convert DataTable to Custom JSON data///</summary>//<param name= "DT" > Data sheet </param>// /<returns>json Strings </returns> public static string Datatabletojson (DataTable dt) {StringBuilder Jsonstrin   g = new StringBuilder (); if (dt! = null && dt. Rows.Count > 0) {list<dictionary<string, string>> List = new list<dictionary<string, string>    > (); for (int i = 0; i < dt. Rows.Count;     i++) {dictionary<string, string> dict = new dictionary<string, string> (); for (int j = 0; j < dt. Columns.count; J + +) {Dict. ADD (dt. COLUMNS[J]. ColumnName, dt. ROWS[I][J].     ToString ()); } list.    ADD (DICT);    } JavaScriptSerializer Jsonserializer = new JavaScriptSerializer ();   return jsonserializer.serialize (list);   } else {return ' {} '; }}///<summary> convert DataTable to JSON///</summary&Gt <param name= "DTB" >Dt</param>///<returns>json string </returns> public static string Dtb2json (   DataTable dtb) {JavaScriptSerializer JSS = new JavaScriptSerializer ();   System.Collections.ArrayList dic = new System.Collections.ArrayList (); if (DTB! = null && DTB. Rows.Count > 0) {foreach (DataRow dr in DtB. Rows) {system.collections.generic.dictionary<string, object> drow = new System.Collections.Generic.Dictionar     Y<string, object> (); foreach (DataColumn dc in DTB. Columns) {Drow. ADD (DC. ColumnName, DR[DC.     ColumnName]); } dic.    ADD (drow); }//serialization of return JSS.   Serialize (DIC);   } else {return ' {} '; }  } }}
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.