ASP.NET中各种连接数据库的配置的方法及json数据转换
一、数据库连接语句
1、MSSQL数据库链接示例
<connectionStrings> <addname="Conn"connectionString="server=.;database=demo;uid=sa;pwd=123456"providerName="System.Data.SqlClient"/> </connectionStrings>
2、Access2003数据库链接示例:"{0}"代表根目录
<connectionStrings> <addname="Conn"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource={0}App_Data/demo.mdb"providerName="System.Data.OleDb"/> </connectionStrings>
Access2007或以上版本的链接
<connectionStrings> <addname="Conn"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource={0}App_Data/demo.mdb"providerName="System.Data.OleDb"/> </connectionStrings>
3、Oracle数据库链接示例
<connectionStrings> <addname="Conn"connectionString="Provider=MSDAORA;DataSource=demo;UserID=sa;Password=123456;"providerName="System.Data.OracleClient"/> </connectionStrings>
4、SQLite数据库链接示例
<connectionStrings> <addname="Conn"connectionString="DataSource={0}App_Data/demo.db;failifmissing=false"providerName="System.Data.SQLite"/> </connectionStrings>
5:MySQL数据库链接示例
<connectionStrings> <addname="Conn"connectionString="host=127.0.0.1;Port=3306;Database=mysql;uid=sa;pwd=12346"providerName="MySql.Data.MySqlClient"/> </connectionStrings>
二、json数据转换
usingSystem; usingSystem.Collections.Generic; usingSystem.Data; usingSystem.Linq; usingSystem.Reflection; usingSystem.Text; usingSystem.Web; usingSystem.Web.Script.Serialization; namespaceRole.DAL { publicclassJson { publicJson(){} ///<summary> ///将datatable数据转换成JSON数据,字符串拼接写成的 ///</summary> ///<paramname="jsonName">json名称。没发现什么作用</param> ///<paramname="dt">得到的数据表</param> ///<returns></returns> publicstringDataTableToJson(stringjsonName,DataTabledt) { StringBuilderJson=newStringBuilder(); Json.Append("["); if(dt.Rows.Count>0) { for(inti=0;i<dt.Rows.Count;i++) { Json.Append("{"); for(intj=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("]"); returnJson.ToString(); } ///<summary> ///列表数据转换到json数据;字符串拼接写成的,太难 ///</summary> ///<typeparamname="T"></typeparam> ///<paramname="jsonName">json名称。没发现什么作用</param> ///<paramname="IL"></param> ///<returns></returns> publicstringObjectToJson<T>(stringjsonName,IList<T>IL) { StringBuilderJson=newStringBuilder(); Json.Append("["); if(IL.Count>0) { for(inti=0;i<IL.Count;i++) { Tobj=Activator.CreateInstance<T>(); Typetype=obj.GetType(); PropertyInfo[]pis=type.GetProperties(); Json.Append("{"); for(intj=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("]"); returnJson.ToString(); } ///<summary> ///将DataTable转化为自定义JSON数据 ///</summary> ///<paramname="dt">数据表</param> ///<returns>JSON字符串</returns> publicstaticstringDataTableToJson(DataTabledt) { StringBuilderJsonString=newStringBuilder(); if(dt!=null&&dt.Rows.Count>0) { List<Dictionary<string,string>>list=newList<Dictionary<string,string>>(); for(inti=0;i<dt.Rows.Count;i++) { Dictionary<string,string>dict=newDictionary<string,string>(); for(intj=0;j<dt.Columns.Count;j++) { dict.Add(dt.Columns[j].ColumnName,dt.Rows[i][j].ToString()); } list.Add(dict); } JavaScriptSerializerjsonSerializer=newJavaScriptSerializer(); returnjsonSerializer.Serialize(list); } else { return"{}"; } } ///<summary> ///将datatable转换为json ///</summary> ///<paramname="dtb">Dt</param> ///<returns>JSON字符串</returns> publicstaticstringDtb2Json(DataTabledtb) { JavaScriptSerializerjss=newJavaScriptSerializer(); System.Collections.ArrayListdic=newSystem.Collections.ArrayList(); if(dtb!=null&&dtb.Rows.Count>0) { foreach(DataRowdrindtb.Rows) { System.Collections.Generic.Dictionary<string,object>drow=newSystem.Collections.Generic.Dictionary<string,object>(); foreach(DataColumndcindtb.Columns) { drow.Add(dc.ColumnName,dr[dc.ColumnName]); } dic.Add(drow); } //序列化 returnjss.Serialize(dic); } else { return"{}"; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。