So Tired !_! 逆水行舟, 不进则退!

27Jan/13

Calling User Defined Database Function From LINQ

Posted by Nick Xu

Entity Framework 4.0 allows you to call a database function both in an esql query and linq to entities. However database function can only return scalar types. Future version would support table valued functions. To use a database function in a query, you have to perform the following steps.

1. Import the function in the store model.

2. To use the function in esql query you must specify the fully qualified namespace of the store model.

3. To make that function available in a linq query, you must define the function with EdmFunction attribute.

To get started we will create a function in the database that returns average product price within that category.

image

Create a new model and import the products table and the above function. Figure below shows our category entity.

image

When you import either a database function or a stored procedure, EF defines the defination of the function using Function element. For database functions, IsComposable is set to true and for stored procedure IsComposable is set to false.

Code below shows the function definition.

image

We can write an esql statement that returns all the products which have a unit price greater then the average price within that category.

image

To use the function in a linq query, we need to define a static method which basically acts as a stub for to call our above function on the store layer. To map the stub method to our function in the store layer, we will use EdmFunction attribute whose first parameter defines the namespace the function resides in and the second parameter is the name of the function.  Code below shows the stub method

image .

Notice the above method throws NotSupportedException because when we call this method in a query, it gets translate to actual call to the store function defined on the SSDL layer. As a result you would never get NotSupportedException thrown at runtime.

Code below shows how to use the above method in a query to get the same result which our esql query gave.

image

9Nov/12

OS X Mountain Lion下Eclipse+opencv的C++开发编译环境配置

Posted by Nick Xu

1.下载源码编译安装opencv

http://opencv.org/

我下载的是2.4.3 for Linux/Mac版本,编译环境需要cmake,可通过brew进来安装

1
2
3
4
5
cd OpenCV-2.4.3
mkdir build
cmake -G "Unix Makefiles" ..
make -j8
sudo make install

opencv的库到此已经安装完成,接下来开始配置eclipse

新建一个c++的空白项目,从opencv源代码的samples/cpp目录下复制drawing.cpp到项目里,然后试一下编译,看看会不会人品爆发编译成功:)

先看一下我的项目结构,main.cpp是我自己把文件名改了,无所谓了

选中项目,按command+i键,进入项目属性对话框

设置完毕后应该可以编译了,还有错误的话根据错误提示慢慢找原因吧。

Tagged as: , , , , No Comments
29Jul/10

The ‘System.Web.Security.SqlMembershipProvider’ requires a database schema compatible with schema version ‘1’.

Posted by Nick Xu

I figured out the problem though and it was a typical bonehead mistake.  I used a script to recreate all the tables for Membership.  I didn't copy any data into those tables.  There is a table called aspnet_schemaversions.  It requires some values to be able to validate the versions (duh).  When I added these values into the table, it started working.

common 1 1
health monitoring 1 1
membership 1 1
personalization 1 1
profile 1 1
role manager 1 1

Chalk it up to the learning curve!

Tagged as: , , 2 Comments
25May/10

C#操作Word完全功略!

Posted by Nick Xu

前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

创建新Word

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

打开文档:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

导入模板

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);

.添加新表

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

.表插入行

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

.单元格合并

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));

.单元格分离

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(

通过段落控制插入

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
Tagged as: , , No Comments
25May/10

C#利用word书签实现模板打印功能

Posted by Nick Xu

C#中的水晶报表好用,但是需要注册码。这里介绍C#调用word直接打印,既方便,又漂亮。

开发指南:

1.  必须安装office的.net开发包。

2.  在工程的reference中添加com组件:Microsoft Word 11.0 Object Library

打印的代码如下:

Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;

object missing = System.Reflection.Missing.Value;
object templateFile = Application.StartupPath + @"表单模版.doc";

try
{
app = new Microsoft.Office.Interop.Word.ApplicationClass();
doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);

try
{
foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
{
bm.Select();

string item = bm.Name;

if (item.Equals("in_time"))
{
bm.Range.Text = table.inTime == null ? "" : table.inTime.ToString();
}
else if (item.Equals("car_id"))
{
bm.Range.Text = table.carID == null ? "" : table.carID.ToString();
}
}
}
catch
{
}

//打印
doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, this.Text);
}

//销毁word进程
finally
{
object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
if (doc != null)
doc.Close(ref saveChange, ref missing, ref missing);

if (app != null)
app.Quit(ref missing, ref missing, ref missing);
}

Tagged as: , , , No Comments
25May/10

C#用 excel 作为模板打印的源码

Posted by Nick Xu

源码

/*
'   //-*****************************************************-//
'  //            如果您要献一份爱心,请参预希望工程!       //
' //                如果您还支持作者,请联系作者!         //
'//-*****************************************************-//
'
'    /// 作 者:长江支流(周方勇)
'    /// Email:MisGoldPrinter@163.com  QQ:150439795
'    /// 网 址:www.webmis.com.cn
'    /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
'
*/

//打印操作,套打、打印、预览
enum PrintFlag
{
/// <summary>
/// 套打,只打印没有印刷的部分
/// </summary>
CasePrint,
/// <summary>
/// 打印全部
/// </summary>
PrintAll,
/// <summary>
/// 预览全部
/// </summary>
PreviewAll
}

//套打、打印、预览三个按钮关联些委托实例
private void Print_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
switch(btn.Tag.ToString())
{
case "套打":
Print(PrintFlag.CasePrint);
break;
case "打印":
Print(PrintFlag.PrintAll);
break;
case "预览":
Print(PrintFlag.PreviewAll);
break;
}
}

private void Print(PrintFlag p_printFlag)
{

//制作步骤:
/*    1、用Excel作出与要打印的样式一样的电子表格存为模板;
*     技巧:最好把第一行与第一列作为空行,以利于调整边距(虽然Excel、打印机可调整页边距), 尽量的在需要调整的地方多空几行与几列,以利于调整套打对准
*
*  2、如同本程序一样,将Excel作为套打的模板,直接将要打印的数据写入;
*
*  3、打印,根据实际的效果调整Excel模板行高列宽及空出的行列, 直到能够准确的套上。将模板拷贝一份,清除模板上的文字与网格线,做成套打的模板。
*/

#region 套打、打印预览

//用Excel打印,步骤为:打开、写数据、打印预览、关闭
GoldPrinter.ExcelExpert.ExcelBase excel = new GoldPrinter.ExcelExpert.ExcelBase();
string strFileName = "invoice.xlt";            //模板文件名

if (p_printFlag == PrintFlag.CasePrint)
{
strFileName = "invoiceCase.xlt";        //套打模板文件名
}

string strExcelTemplateFile = System.IO.Path.GetFullPath(@"....ExcelTemplate" + strFileName);

excel.Open(strExcelTemplateFile);                            // 用模板文件
excel.Visible = false;                                        //建议:如果excel不可见且在编程情况下写数据特 别是大量数据时
excel.ScreenUpdating = false;                                //设置此开关能大大提高效率。写完后如要可见,再设置此属性为真刷新屏 幕。
excel.Caption = "税 务 机 关 代 开 统 一 发 票(国 税)";    //"MIS金质打印通  通打天下报表";

//在模板中写入要打印的数据

//***发票抬头***

//年月日
excel.SetCellText(7,"B",txtYear.Text + "年" + txtMonth.Text + "月" + txtDay.Text + "日" );

//付款方名称
excel.SetCellText(8,"D",txtPayer.Text);
//收款方名称
excel.SetCellText(9,"D",txtCollecter.Text);
//及地址、电话
excel.SetCellText(11,"D",txtCollecterAddTel.Text);

// 代开普通发票    申 请 表 号 码
excel.SetCellText(8,"J",txtInvoiceApplicationNo.Text);
//收款方识别号或 证 件 号 码
excel.SetCellText(9,"J",txtCollecterID.Text);

//***品名及金额、备注***
//B14到B23是品名   F14到F23为金额

excel.SetCellText("B14",txtP1.Text);
excel.SetCellText("F14",txtJ1.Text);

excel.SetCellText("B15",txtP2.Text);
excel.SetCellText("F15",txtJ2.Text);

excel.SetCellText("B16",txtP3.Text);
excel.SetCellText("F16",txtJ3.Text);

excel.SetCellText("B17",txtP4.Text);
excel.SetCellText("F17",txtJ4.Text);

excel.SetCellText("B18",txtP5.Text);
excel.SetCellText("F18",txtJ5.Text);

excel.SetCellText("B19",txtP6.Text);
excel.SetCellText("F19",txtJ6.Text);

excel.SetCellText("B20",txtP7.Text);
excel.SetCellText("F20",txtJ7.Text);

excel.SetCellText("B21",txtP8.Text);
excel.SetCellText("F21",txtJ8.Text);

excel.SetCellText("B22",txtP9.Text);
excel.SetCellText("F22",txtJ9.Text);

excel.SetCellText("B23",txtP10.Text);
excel.SetCellText("F23",txtJ10.Text);

//备注
//excel.SetCellText(14,"I",txtMemo.Imag.);

//***发票总金额***

//合计人民币   (大写)
excel.SetCellText(24,"D",txtTotalUpper.Text);
//合计人民币   (小写)
excel.SetCellText(24,"K",txtTotalLower.Text);

//税额   (大写)
excel.SetCellText(25,"D",txtTaxUpper.Text);
//税额   (小写)
excel.SetCellText(25,"L",txtTaxLower.Text);

//***发票尾***
//税控码
excel.SetCellText(26,"C",txtTaxControlCode.Text);
//开票人:
excel.SetCellText(26,"H",txtWriter.Text);

//刷新Excel屏幕
excel.ScreenUpdating = true;

if (p_printFlag == PrintFlag.CasePrint || p_printFlag == PrintFlag.PrintAll)
{
excel.Print();                //打印
}
else
{
excel.PrintPreview();        //预览
}

excel.Close();                    //关闭并释放

#endregion

}

private void frmInvoice_Load(object sender, System.EventArgs e)
{
//初始当日日期
System.DateTime dt = System.DateTime.Now;
SetToday(dt);
}

private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}

//回车
private void frmInvoice_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
SendKeys.Send("{TAB}");
}
}

//金额小写转人民币大写
private void txtTotalLower_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
SetUpperMoney();
}

//重新总计
private void txtJX_TextChanged(object sender, System.EventArgs e)
{
double dblMoney = 0;

dblMoney += GetInputMoney(txtJ1.Text);
dblMoney += GetInputMoney(txtJ2.Text);
dblMoney += GetInputMoney(txtJ3.Text);
dblMoney += GetInputMoney(txtJ4.Text);
dblMoney += GetInputMoney(txtJ5.Text);
dblMoney += GetInputMoney(txtJ6.Text);
dblMoney += GetInputMoney(txtJ7.Text);
dblMoney += GetInputMoney(txtJ8.Text);
dblMoney += GetInputMoney(txtJ9.Text);
dblMoney += GetInputMoney(txtJ10.Text);

txtTotalLower.Text = dblMoney.ToString();
SetUpperMoney();

}

//改变税率重算
private void cboTaxRate_TextChanged(object sender, System.EventArgs e)
{
SetUpperMoney();
}

private void btnRefDate_Click(object sender, System.EventArgs e)
{
cldSelect.Visible = true;
cldSelect.SetDate(new DateTime(int.Parse(txtYear.Text),int.Parse(txtMonth.Text),int.Parse(txtDay.Text)));
cldSelect.Focus();
}

private void cldSelect_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
SetToday(e.End);
cldSelect.Visible = false;
}

//大写合计人民币、税额
private void SetUpperMoney()
{
try
{
//                string strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text));
//                //合计人民币
//                txtTotalUpper.Text = strUpper;
//
//                strUpper = GoldPrinter.ExcelExpert.ChineseNum.GetUpperMoney(Double.Parse(txtTotalLower.Text) * Double.Parse(cboTaxRate.Text) / 100);
//                //税额 = 合计人民币 * 税率
//                txtTaxUpper.Text = strUpper;

}
catch{}
}

private double GetInputMoney(string p_text)
{
double dblReturn = 0;
try
{
dblReturn = double.Parse(p_text);
}
catch{}

return dblReturn;
}

private void SetToday(System.DateTime dt)
{
txtYear.Text = dt.Year.ToString();

txtMonth.Text = GetLengthTwoDate(dt.Month.ToString());
txtDay.Text = GetLengthTwoDate(dt.Day.ToString());
}

private string GetLengthTwoDate(string p_MonthOrDay)
{
string strReturn = p_MonthOrDay;
if (strReturn.Length == 1)
{
strReturn = "0" + strReturn;
}

return strReturn;
}

}//End Class
}//End Namespace

Tagged as: , , , No Comments
25May/10

C#调用QQ的DLL实现截图功能

Posted by Nick Xu

今天写个程序 需要抓屏 懒得写代码 上网找
突然想到QQ的抓屏不错 直接拿来用就是了

[ DllImport( "CameraDll.dll", EntryPoint="CameraSubArea", CharSet=CharSet.Ansi )]
private static extern bool CameraSubArea();

if(CameraSubArea())
{
IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
if(iData.GetDataPresent("System.Drawing.Bitmap",true))
{
this.pictureBox1.Image=iData.GetData("System.Drawing.Bitmap") as Bitmap;
}
}

Tagged as: , , No Comments
   
site
site