2013年7月16日 星期二

ASP.NET 透過ODBC連結MySQL

MySQL ODBC Driver:

安裝完成後,便可在ODBC上查詢到值:

SNAGHTML182edfe

PS:如果不清楚版本,那就32與64都安裝。

程式:

public DataTable GetMyDbList()
{
OdbcConnection conn;
OdbcDataAdapter ap;
DataSet ds = new DataSet();
string constr = "DRIVER={MySQL ODBC 5.2 ANSI Driver};SERVER=localhost;DATABASE=test;UID=root;PWD=;OPTION=3";
conn = new OdbcConnection(constr);
conn.Open();
string cmdstr = "select * from mydb;";
ap = new OdbcDataAdapter(cmdstr, conn);
ap.Fill(ds);
return ds.Table[0];
}
view raw mysqlodbc.cs hosted with ❤ by GitHub

因為DRIVER安裝版本不同,名稱也會有所不同,
DRIVER={MySQL ODBC 5.2 ANSI Driver},紅色部分就是安裝後ODBC上顯示的名稱。

以上圖為例,可表示為:
DRIVER={MySQL ODBC 5.2 ANSI Driver}
DRIVER={MySQL ODBC 5.2 Unicode Driver}

 

部署IIS注意事項:

  • 測試 OS:WIN7或以上(64位元環境)
  • 若是安裝的DRIVER版本為32位元,需要將應用程式集區的[啟用32位元應用程式]屬性改為True;64位元則使用預設值(False)。

SNAGHTML1962e0a

2013年7月5日 星期五

Sitefinity CMS-Part04-使用WCF Webservice

Sitefinity除了提供Library讓使用者開發外,系統還提供wcf webservice。

參考:List of Web Services

以"pagesservice.svc"為例:

網址會回傳json檔,以下實作jquery取得json。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
function GetJsonData(webserviceUrl, inputJson) {
var res;
$.ajax({
type: "Get"
, url: webserviceUrl
, dataType: "json"
, contentType: "application/json;charset-utf-8"
, async: false
/*, data: inputJson*/
, success: function (response) {
res = response;
}
, error: function () { res = "error"; }
});
return res;
}
$(function () {
var webserviceUrl = "http://localhost/SitefinityCMS/Sitefinity/services/pages/pagesservice.svc/";
var inputJson = "";
var res = GetJsonData(webserviceUrl, inputJson);
$.each(res, function (key, item) {
if (key == "Items") {
$.each(item, function (key2, item2) {
if (key2 == "1") {
var str = "";
$.each(item2, function (key3, item3) {
str = str +"<li>"+ key3 + ":" + item3 + "</li>";
});
var obj = $("#main");
$(str).appendTo(obj);
}
});
}
});
/*var data = $.parseJSON(res);*/
/*alert(data);*/
});
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>

結果:

image

Sitefinity CMS-Part03-自訂模組(widget)

參考影片: sitefinity官網

自訂模組的功能需要特定權限才可以進行開發。

在settings中,有自訂模組的功能,定義模組(usercontrol)的路徑。

image

設定好模組後,在頁面設定的功能,Custom下就可以選到自定義的模組。

image

Demo:

image

後臺內容管理:

image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity;
namespace SitefinityWebApp.custom
{
public partial class ucBlog : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
}
protected void btnCreateBlog_Click(object sender, EventArgs e)
{
Telerik.Sitefinity.App.WorkWith().Blog().CreateNew().Do(b => b.Title = "My New Blog").SaveChanges();
}
protected void btnCreatePosts_Click(object sender, EventArgs e)
{
using (var fluent = App.WorkWith())
{
var blog = (from b in fluent.Blogs()
where b.Title == "My New Blog"
select b).First();
for (int i = 0; i < 10; i++)
{
blog.CreateBlogPost().Do(p => p.Title = "Blog Post" + i).SaveChanges();
}
}
}
protected void DisplayPosts_Click(object sender, EventArgs e)
{
var posts = from p in App.WorkWith().BlogPosts()
where p.Parent.Title == "My New Blog"
orderby p.Title
select p;
this.gvList.DataSource = posts.Get();
this.gvList.DataBind();
}
}
}
view raw ucBlog.ascx hosted with ❤ by GitHub