首页 >后端开发 >C#.Net教程 >配置数据库连接和json转换

配置数据库连接和json转换

Y2J
Y2J原创
2017-05-06 11:59:301692浏览

本篇文章主要介绍了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基础视频教程

以上是配置数据库连接和json转换的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn