您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)連接和json轉(zhuǎn)換,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
本篇文章主要介紹了ASP.NET中各種連接數(shù)據(jù)庫(kù)的配置的方法,詳細(xì)的介紹了MSSQL、Access、Oracle、SQLite、MySQL數(shù)據(jù)庫(kù)配置,具有一定的參考價(jià)值,有興趣的可以了解一下。
一、數(shù)據(jù)庫(kù)連接語(yǔ)句
1、MSSQL數(shù)據(jù)庫(kù)鏈接示例
<connectionStrings> <add name="Conn" connectionString="server=.;database=demo;uid=sa;pwd=123456" providerName="System.Data.SqlClient"/> </connectionStrings>
2、Access 2003數(shù)據(jù)庫(kù)鏈接示例:"{0}"代表根目錄
<connectionStrings> <add name="Conn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}App_Data/demo.mdb" providerName="System.Data.OleDb"/> </connectionStrings>
Access 2007或以上版本的鏈接
<connectionStrings> <add name="Conn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}App_Data/demo.mdb" providerName="System.Data.OleDb"/> </connectionStrings>
3、Oracle 數(shù)據(jù)庫(kù)鏈接示例
<connectionStrings> <add name="Conn" connectionString="Provider=MSDAORA;Data Source=demo;User ID=sa;Password=123456;" providerName="System.Data.OracleClient"/> </connectionStrings>
4、SQLite 數(shù)據(jù)庫(kù)鏈接示例
<connectionStrings> <add name="Conn" connectionString="Data Source={0}App_Data/demo.db;failifmissing=false" providerName="System.Data.SQLite"/> </connectionStrings>
5:MySQL數(shù)據(jù)庫(kù)鏈接示例
<connectionStrings> <add name="Conn" connectionString="host=127.0.0.1;Port=3306;Database=mysql;uid=sa;pwd=12346" providerName="MySql.Data.MySqlClient"/> </connectionStrings>
二、json數(shù)據(jù)轉(zhuǎn)換
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> ///將datatable數(shù)據(jù)轉(zhuǎn)換成JSON數(shù)據(jù), 字符串拼接寫(xiě)成的 /// </summary> /// <param name="jsonName">json名稱(chēng)。沒(méi)發(fā)現(xiàn)什么作用</param> /// <param name="dt">得到的數(shù)據(jù)表</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> /// 列表數(shù)據(jù)轉(zhuǎn)換到j(luò)son數(shù)據(jù);字符串拼接寫(xiě)成的,太難 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName">json名稱(chēng)。沒(méi)發(fā)現(xiàn)什么作用</param> /// <param name="IL"></param> /// <returns></returns> public string ObjectToJson<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> /// 將DataTable轉(zhuǎn)化為自定義JSON數(shù)據(jù) /// </summary> /// <param name="dt">數(shù)據(jù)表</param> /// <returns>JSON字符串</returns> public static string DataTableToJson(DataTable dt) { StringBuilder JsonString = 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> /// 將datatable轉(zhuǎn)換為json /// </summary> /// <param name="dtb">Dt</param> /// <returns>JSON字符串</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.Dictionary<string, object>(); foreach (DataColumn dc in dtb.Columns) { drow.Add(dc.ColumnName, dr[dc.ColumnName]); } dic.Add(drow); } //序列化 return jss.Serialize(dic); } else { return "{}"; } } } }
看完上述內(nèi)容,你們對(duì)如何實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)連接和json轉(zhuǎn)換有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。