欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中各种连接数据库的配置的方法及json数据转换

程序员文章站 2022-04-10 08:33:43
一、数据库连接语句 1、mssql数据库链接示例

一、数据库连接语句

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 "{}";
   }
  }

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。