設定資料庫串連和json轉換

來源:互聯網
上載者:User
本篇文章主要介紹了ASP.NET中各種串連資料庫的配置的方法,詳細的介紹了MSSQL、Access、Oracle、SQLite、MySQL資料庫配置,具有一定的參考價值,有興趣的可以瞭解一下。

一、資料庫連接語句

1、MSSQL資料庫連結樣本

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

2、Access 2003資料庫連結樣本:"{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 資料庫連結樣本

 <connectionStrings> <add name="Conn" connectionString="Provider=MSDAORA;Data Source=demo;User ID=sa;Password=123456;" providerName="System.Data.OracleClient"/> </connectionStrings>

4、SQLite 資料庫連結樣本

 <connectionStrings> <add name="Conn" connectionString="Data Source={0}App_Data/demo.db;failifmissing=false" providerName="System.Data.SQLite"/> </connectionStrings>

5:MySQL資料庫連結樣本

 <connectionStrings> <add name="Conn" connectionString="host=127.0.0.1;Port=3306;Database=mysql;uid=sa;pwd=12346" providerName="MySql.Data.MySqlClient"/> </connectionStrings>

二、json資料轉換

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資料轉換成JSON資料, 字串拼接寫成的  /// </summary>  /// <param name="jsonName">json名稱。沒發現什麼作用</param>  /// <param name="dt">得到的資料表</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>  /// 列表資料轉換到json資料;字串拼接寫成的,太難  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="jsonName">json名稱。沒發現什麼作用</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轉化為自訂JSON資料  /// </summary>  /// <param name="dt">資料表</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轉換為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 "{}";   }  } }}

【相關推薦】

1. ASP免費視頻教程

2. ASP教程

3. 李炎恢ASP基礎視頻教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.