Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Thursday, January 14, 2010

mysql秒數轉時間

秒數以1970-01-01 00:00:00為基準,在加上距離至現在的秒數

秒數轉時間:FROM_UNIXTIME(‘欄位名稱’)

時間轉秒數:SELECT UNIX_TIMESTAMP( '2010/01/01 11:11:11' )

Tuesday, April 28, 2009

轉換sql datetime為RFC 822格式

SET LANGUAGE 'English';
select  left(datename( dw, getutcdate() ), 3 )  + ', ' +convert( varchar(20), getutcdate(), 113 ) + ' GMT'
SET LANGUAGE 'Traditional Chinese'

由於使用的是'繁體中文'語系,再轉換前還需要將語系改為'英文'

 

附註: .net datetime轉RFC 822格式

DateTime today = DateTime.Now;
CulturInfo ci = new Culturinfo("en-US");
string rfc822 = today.ToString("ddd, dd MMM yyyy HH:mm:ss",ci)+" +0800";

Monday, April 13, 2009

儲存doc至資料庫

1. 上傳doc後,讀入bytes至陣列中,儲存bytes[]至資料庫的varbinary(MAX)。
2. 輸出的時候,從varbinary(MAX)讀出bytes[],response輸出二進位字元,設定標頭為"Content-disposition", "inline; filename=xxx.doc", contenttype為application/msword。

寫入

protected void UploadButton_Click(Object sender , EventArgs e ){   // Handles UploadButton.Click
Stream stream = FileUpload1.FileContent;
BinaryReader binReader = new BinaryReader(stream);

byte[] bytes = new byte[stream.Length];
int numBytesToRead = (int)stream.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = stream.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
}
public string insertDoc(byte[] bytes)
{
.....
sqlCommand.Parameters.Add("@doc", SqlDbType.VarBinary, -1).Value = bytes;
......
}

輸出
protected void Page_Load(object sender, EventArgs e)
{
DBAccess dba = new DBAccess();
byte[] docContent = dba.getDoc();

string fileName = "kkk我.doc";
if (Request.Browser.Browser == "IE")
{
fileName = Server.UrlPathEncode(fileName);
}
Response.ContentType = "application/msword";
Response.AppendHeader("Content-disposition", "inline; filename=" + fileName);
Response.BinaryWrite(docContent);

}