參考網址
https://www.dotblogs.com.tw/mis2000lab/archive/2015/06/29/npoi_2131_sample_export_to_excel.aspx
工作備忘錄
2015年12月16日 星期三
2015年8月21日 星期五
[C#]使用泛型實作Entity Framework DbContext的增修刪查
參考
利用EF和C#泛型实现通用分页查询
Entity與DataTable互轉
Entity Framework 5 Updating a Record
回傳結果
public class Return_Msg
{
public bool Status;
public string Message;
public Return_Msg(bool status, string message)
{
Status = status;
Message = message;
}
}
1.新增
public Return_Msg InsertData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Add(entity);
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.InsertData<Sys_Menu>(menu);
2.修改
public Return_Msg UpdateData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Attach(entity);
var entry = context.Entry(entity);
context.Entry(entity).State = EntityState.Modified;
// other changed properties
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.UpdateData<Sys_Menu>(menu);
3.刪除
public Return_Msg DeleteData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Remove(entity);
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.DeleteData<Sys_Menu>(menu);
4.查詢
依條件查詢
public DataTable GetDataByWhere<T>(Expression<Func<T, bool>> predicate) where T : class
{
using (var context = new DbContext())
{
return EntityToDataTable<T>(context.Set<T>().Where<T>(predicate).AsEnumerable<T>());
}
}
用法:DataTable dt = dbService.GetDataByWhere<Sys_Menu>(p => p.MenuName.Contains(txtMenuName.Text));
單一Table全部資料
public DataTable GetDataAll<T>() where T : class
{
using (var context = new HDContext())
{
return EntityToDataTable<T>(context.Set<T>().AsEnumerable());
}
}
用法:DataTable dt = dbService.GetDataAll<Sys_Menu>();
利用EF和C#泛型实现通用分页查询
Entity與DataTable互轉
Entity Framework 5 Updating a Record
回傳結果
public class Return_Msg
{
public bool Status;
public string Message;
public Return_Msg(bool status, string message)
{
Status = status;
Message = message;
}
}
1.新增
public Return_Msg InsertData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Add(entity);
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.InsertData<Sys_Menu>(menu);
2.修改
public Return_Msg UpdateData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Attach(entity);
var entry = context.Entry(entity);
context.Entry(entity).State = EntityState.Modified;
// other changed properties
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.UpdateData<Sys_Menu>(menu);
3.刪除
public Return_Msg DeleteData<T>(T entity) where T : class
{
Return_Msg r = new Return_Msg(true, string.Empty);
using (var context = new DbContext())
{
try
{
context.Set<T>().Remove(entity);
context.SaveChanges();
}
catch (Exception e)
{
r.Status = false;
r.Message = e.ToString();
}
}
return r;
}
用法:Return_Msg r = dbService.DeleteData<Sys_Menu>(menu);
4.查詢
依條件查詢
public DataTable GetDataByWhere<T>(Expression<Func<T, bool>> predicate) where T : class
{
using (var context = new DbContext())
{
return EntityToDataTable<T>(context.Set<T>().Where<T>(predicate).AsEnumerable<T>());
}
}
用法:DataTable dt = dbService.GetDataByWhere<Sys_Menu>(p => p.MenuName.Contains(txtMenuName.Text));
單一Table全部資料
public DataTable GetDataAll<T>() where T : class
{
using (var context = new HDContext())
{
return EntityToDataTable<T>(context.Set<T>().AsEnumerable());
}
}
用法:DataTable dt = dbService.GetDataAll<Sys_Menu>();
2015年8月6日 星期四
2015年7月19日 星期日
[C#]取得類別成員的屬性
參考
How do I get the custom attribute value of a field? [duplicate]
以取得字串最大長度為例子,取得成員的StringLength屬性
程式碼如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace RetriveClassAttribute
{
class Program
{
static void Main(string[] args)
{
foreach (var prop in typeof(RetriveClassAttribute).GetProperties())
{
var attrs = (StringLengthAttribute[])prop.GetCustomAttributes
(typeof(StringLengthAttribute), false);
foreach (var attr in attrs)
{
Console.WriteLine("{0}: {1}", prop.Name, attr.MaximumLength);
}
}
Console.ReadLine();
}
}
class RetriveClassAttribute
{
[StringLength(30)]
public string FirstMember { get; set; }
}
}
How do I get the custom attribute value of a field? [duplicate]
以取得字串最大長度為例子,取得成員的StringLength屬性
程式碼如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace RetriveClassAttribute
{
class Program
{
static void Main(string[] args)
{
foreach (var prop in typeof(RetriveClassAttribute).GetProperties())
{
var attrs = (StringLengthAttribute[])prop.GetCustomAttributes
(typeof(StringLengthAttribute), false);
foreach (var attr in attrs)
{
Console.WriteLine("{0}: {1}", prop.Name, attr.MaximumLength);
}
}
Console.ReadLine();
}
}
class RetriveClassAttribute
{
[StringLength(30)]
public string FirstMember { get; set; }
}
}
2015年7月15日 星期三
[C#]WCF Service 傳遞XML Data
參考
逐步解說:建立和存取 WCF 服務
WCF服務程式庫與WCF服務應用程式的不同
WCF服務應用程式原始碼
ISendXmlWcfService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
namespace SendXmlWcfService
{
public class SendXmlWcfService : ISendXmlWcfService
{
public string GetData(string value)
{
XDocument xml = XDocument.Parse(value);
return string.Format("To {0}\r\n {1}\r\n {2} {3}", xml.Root.Element("to").Value, xml.Root.Element("body").Value,xml.Root.Element("heading").Value,xml.Root.Element("from").Value);
}
}
}
逐步解說:建立和存取 WCF 服務
WCF服務程式庫與WCF服務應用程式的不同
WCF服務應用程式原始碼
ISendXmlWcfService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
namespace SendXmlWcfService
{
public class SendXmlWcfService : ISendXmlWcfService
{
public string GetData(string value)
{
XDocument xml = XDocument.Parse(value);
return string.Format("To {0}\r\n {1}\r\n {2} {3}", xml.Root.Element("to").Value, xml.Root.Element("body").Value,xml.Root.Element("heading").Value,xml.Root.Element("from").Value);
}
}
}
ISendXmlWcfService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace SendXmlWcfService
{
[ServiceContract]
public interface ISendXmlWcfService
{
[OperationContract]
string GetData(string value);
}
}
測試WCF服務應用程式-主控台應用程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StringBuilder builder = new StringBuilder("<note>" +
"<to>Tove</to>" +
"<from>Jani</from>" +
"<heading>Reminder</heading>" +
"<body>Don't forget me this weekend!</body>" +
"</note>");
ServiceReference1.SendXmlWcfServiceClient client = new ServiceReference1.SendXmlWcfServiceClient();
Console.WriteLine(client.GetData(builder.ToString()));
Console.ReadLine();
}
}
}
2015年7月8日 星期三
[C#]WebRequest 遠端伺服器傳回一個錯誤: (407) 必須 Proxy 驗證
參考
[WCF]遠端伺服器傳回未預期的回應: (407) Proxy Authentication Reqiured
在Web.config或App.config內加上
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>
[WCF]遠端伺服器傳回未預期的回應: (407) Proxy Authentication Reqiured
在Web.config或App.config內加上
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>
訂閱:
文章 (Atom)