2012年4月26日 星期四

【ASP.NET】讀取ASP Cookies 方式


【ASP.NET】讀取ASP Cookies 方式

            if (Request.Cookies["UserID"] != null)
            {
                string userSettings;
                if (Request.Cookies["UserID"] != null){
                    userSettings = Request.Cookies["UserID"].Value;
                    Label3.Text = userSettings;
                }
            }

參考網址:http://msdn.microsoft.com/zh-tw/library/bd70eh18(v=vs.80).aspx

2012年4月24日 星期二

【ASP.NET C#】SqlDataSource InsertCommand 空白 Null 參數設定 日期

SqlDataSource InsertCommand 新增 空白 Null 參數 設定 日期 或其型態,新增資料庫看到是 Null 尚未填寫,而不是空白,在日期欄位就不會發生錯誤

以下適用 SqlDataSource  Insert Update 功能

我有拉了一個 SqlDataSource 進來,而不是自己在Code 建一個Command

(第一種做法) 在Code 裡面 建立 Parameter 類別參數,設定該參數型別及值

SqlDataSource1.InsertCommand = "Insert Into TABLE2(id,tdate) Values('test1',@strDate) ";

Parameter strDate = new Parameter("strDate", System.Data.DbType.DateTime,string.Empty);

SqlDataSource1.InsertParameters.Add(strDate);

SqlDataSource1.Insert();


(第二種做法) 直接在InsertParameters 使用
SqlDataSource1.InsertCommand = "Insert Into TABLE2(id,tdate) Values('test1',@strDate) ";


SqlDataSource1.InsertParameters.Add(new Parameter("strDate", TypeCode.DateTime, string.Empty));

SqlDataSource1.Insert();

(第三種做法) 予許 變數 為 Null 型別 ConvertEmptyStringTo=True
SqlDataSource1.InsertCommand = "Insert Into TABLE2(id,tdate) Values('test1',@strDate) ";

Parameter strDate = new Parameter("strDate", System.Data.DbType.DateTime);

strDate.ConvertEmptyStringToNull = true;

SqlDataSource1.InsertParameters.Add(strDate);

SqlDataSource1.Insert();

(第四種做法)我還沒用過,因為他是建立 sqlcommand 類別

Command.Parameters.Add(new SqlParameter("@EditTime", SqlDbType.DateTime));
Command.Parameters["@EditTime"].IsNullable = true;
if((EditTime == null) || (EditTime == DateTime.MinValue))
Command.Parameters["@EditTime"].Value = DBNull.Value;
else
Command.Parameters["@EditTime"].Value = EditTime;
此做法
參考網址1:http://tw.myblog.yahoo.com/pey-chow/article?mid=8&prev=9&next=7
參考網址2:http://www.programmer-club.com/ShowSameTitleN/aspdotnet/5.html


另外介紹: Parameter 類別 重要參數設定 
一、public Parameter(string name, DbType dbType);

二、public Parameter(string name, DbType dbType, string defaultValue);

因為Parameter 只吃 String(字串)格式 所以我們使用string.Empty使用方式帶入,無法使用('')或toString()方式,因為型別(Date)不一樣,在 Insert它不吃('')及toString(),會出現型態格式錯誤

型別 使用:System.Data.DbType.DateTime 或 TypeCode.DateTime 都可使用