測試文字功能,小工具,標題

記錄很重要,不然會浪費很多時間在找以前的記憶

一個人的氣度,決定他未來的高度。

2013年11月5日 星期二

認識Char

認識Char/NChar/VarChar/NVarChar/Text/NText


其實這個的識別方式很簡單.Char/NChar/VarChar/NVarChar/Text/NText,這幾個資料型別或許有些人很熟,有些人只熟其中幾個,尤其是程式設計師,不碰DB Design的,如果有碰到,也可能依"慣例"的去設定資料型別,所以不難看見,有些Table資料型別很單調,看的到的型別不多,只要是存文字的,都是NVarChar,或許在瞭解後,可以做出更好的規劃.
帶Var的 : 代表就是非固定長度的型別,所以Var在存不固定長度的資料時,會較省空間,
帶N的 : 就是支援Unicode,但要注意,儲存的資料會大兩倍.
這樣就把Char全部講完,剩Text跟Char有什麼不同,Text的特性跟VarChar一樣,只是用來儲存大量的文字資料,但後來的VarChar也做了調整,也可以存大量的文字資料,所以Text在未來新版的DB會移除.

Char的長度為1~8000
VarChar的長度為1~8000,如果是VarChar(max)就是2^31-1 位元組,VarChar(max)只有SQL2005(含)以上才支援,SQL2000就沒有.
Text的長度為2^31-1,它的長度跟VarChar(max)是一樣的.

在MSDN的Char與VarChar的備註有點問題:
如果您有支援多國語言的網站,請考慮利用 Unicode nchar 或 nvarchar 資料類型,將字元轉換問題減到最少。如果您使用 char 或 varchar,我們建議您執行下列動作:
  • 當資料行資料項目的大小不一致時,請使用 char。
  • 當資料行資料項目的大小變化相當大時,請使用 varchar。
  • 當資料行資料項目的大小變化相當大,且大小可能超出 8,000 位元組時,請使用 varchar(max)。
紅色的那一段,應該是”當資料行資料項目的大小一致時,請使用char。”
見英文版:
Use char when the sizes of the column data entries are consistent.

在底下有個Data Type Performance Tuning Tips for Microsoft SQL Server,這是個不錯的參考網站~
當然DB裡還有相當多的型別,這裡就只大概針對Char做了一點說明,其它就參考MSDN上的說明.

參考 :

2013年7月12日 星期五

使用GMAIL寄信

使用GMAIL寄信

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class sendGmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 MailMessage msg = new MailMessage();
        msg.From = new MailAddress("寄件者帳號@gmail.com", "寄件者顯示名稱"); //
        msg.To.Add(new MailAddress("收件人的MAIL帳號@livemail.tw", "收件者顯示名稱"));  //可寫迴圈多筆寄送

        msg.Subject="主旨";
        msg.IsBodyHtml=true;

        string str ="<h2>內文1</h2>";
        str += "<h3>內文2</h3>";
        str += "<h3>543</h3>";   // html上的東西都能收,圖檔要注意路徑,http://網址/資料夾/檔名,也能放超連結

        msg.Body = str;
        // msg.Attachments //附件
        // msg.CC //副本

       SmtpClient clinet= new SmtpClient("smtp.gmail.com");

       ////可用asp.net組態,管理工具設定,下面可以不用自己寫,但要手動去 Web.config 加上 enableSsl = true;
       NetworkCredential loginInfo = new NetworkCredential("gmail帳號@gmail.com", "gmail帳號的密碼");


       clinet.UseDefaultCredentials = false;
       clinet.Credentials = loginInfo;
       clinet.EnableSsl = true;

     ////  clinet.Send("寄件者帳號@gmail.com", "收件者帳號@livemail.tw", "主旨", "內文"); //單筆方式

       clinet.Send(msg);
   }
}

////Web.config會加上這段,自己加上 enableSsl = "true"
<!--
</connectionStrings><system.net>
    <mailSettings>
      <smtp from="寄件者的顯示@gmail.com">
        <network host="smtp.gmail.com" password="gamil密碼" userName="gmail帳號" enableSsl = "true"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
-->

2013年7月9日 星期二

Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class M4_linqEnt : System.Web.UI.Page
{
    NorthwindModel.NorthwindEntities northwindEntitie = new NorthwindModel.NorthwindEntities();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            show();
        }
    }

    protected void show()
    {
        var _shippers = from s in northwindEntitie.Shippers
                        select s;
        foreach (var s in _shippers)
        {
            Response.Write(s.ShipperID + " , " + s.CompanyName + " , "+s.Phone + "<br>");
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        NorthwindModel.Shipper _shipper = new NorthwindModel.Shipper();  //新增,要建立一個新的
        _shipper.CompanyName = "123123";
        _shipper.Phone = "0202020202";
        northwindEntitie.Shippers.AddObject(_shipper); //新增add
        northwindEntitie.SaveChanges();
        show();
    }
    protected void btnup_Click(object sender, EventArgs e)
    {
      int txtb1 = int.Parse(TextBox1.Text);
        NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers //修改,要先查詢出資料
                                           where s.ShipperID == txtb1
                                           select s).Single();
        _shipper.CompanyName = "111111";
        _shipper.Phone = "0202-0202";
        northwindEntitie.SaveChanges(); //修改直接存檔
        show();
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        int txtb1 = int.Parse(TextBox1.Text);
        NorthwindModel.Shipper _shipper = (from s in northwindEntitie.Shippers // 刪除也要先查出資料
                                           where s.ShipperID == txtb1
                                           select s).Single();
        northwindEntitie.Shippers.DeleteObject(_shipper); // 刪除DeleteObject
        northwindEntitie.SaveChanges(); //存檔
        show();
    }
}

DataReader


private DataTable RunSqlString()
        {
            using (SqlConnection conn = new SqlConnection(textBox1.Text))
            {
                DataTable dt = null;
                String strCmd = textBox2.Text;// "Select md_date,md_room from meeting_date where md_date=@md_date and md_room=@md_room ";
                using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                {
                    //cmd.Parameters.AddWithValue("@md_date", strdate);
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr.HasRows)
                            {
                                dt = new DataTable("tmp");

                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    dt.Columns.Add(dr.GetName(i));
                                }

                                while (dr.Read())
                                {
                                    DataRow drw = dt.NewRow();
                                    for (int i = 0; i < dr.FieldCount; i++)
                                    {
                                        drw[i] = dr[i].ToString();
                                    }
                                    dt.Rows.Add(drw);
                                }
                            }
                            else
                            {
                                MessageBox.Show("沒有資料!!!");
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                        throw;
                    } //要先開啟連線
                    finally
                    {
                        conn.Close();
                    }
                }
                return dt;
            }
        }//結尾

=======================================================================
   
protected bool check_meetingroom_pk(string strdate ,string strroom ) {
        bool b = false;
        //步驟一給予適當的名稱空間namespace      using System.Data.SqlClient;
        //步驟二建立連線物件   
        String strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        using (SqlConnection conn = new SqlConnection(strConn))
        {
            //步驟三建立Command物件讀取資料庫的資料
            String strCmd = "Select md_date,md_room from meeting_date where md_date=@md_date and md_room=@md_room ";
            using (SqlCommand cmd = new SqlCommand(strCmd, conn))
            {
                cmd.Parameters.AddWithValue("@md_date", strdate);
                cmd.Parameters.AddWithValue("@md_room", strroom);
                //步驟四建立DataReader物件處理讀出來的資料
                //透過Command物件所提供的ExecuteReader的方法來產生Command
                //要先開啟連線
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                //步驟五在瀏覽器上顯示資料
                //讀取資料之前要先呼叫read()的方法
                while (dr.Read())
                {
                    b = true; // dr[0].ToString();
                }
                conn.Close();
            }
        }
        return b;
    }

DataAdapter


 String strConn = "data source=.;user id=aa;password=aa;initial Catalog=northwind";
        using (SqlConnection conn = new SqlConnection(strConn))
        {
            String strCmd = "Select ProductID,ProductName from Products where categoryID=@categoryID";
            using (SqlDataAdapter da = new SqlDataAdapter(strCmd, conn))
            {
                da.SelectCommand.Parameters.AddWithValue("@categoryID", 1);
                DataSet ds = new DataSet();
                da.Fill(ds, "dsProducts");

                ////新增,刪除,修改才要用
                //SqlCommandBuilder builder = new SqlCommandBuilder(da);
                ////新增
                //builder.GetInsertCommand();

                //DataTable productsTable = ds.Tables["dsProducts"];//修改會用這這行
                //DataRow newRow = productsTable.NewRow();
                //newRow["ProductName"] = "IP5S";
                //newRow["ProductID"] = "1";
                //productsTable.Rows.Add(newRow);
                         

                ////修改
                //builder.GetUpdateCommand();
                //productsTable.Rows[1]["ProductName"] = "KEVIN";
               

                ////刪除
                //builder.GetDeleteCommand();
                //ds.Tables["dsProducts"].Rows[12].Delete();

                ////最後更新
                //da.Update(ds, "dsProducts");  //最後更新


                //查詢用到的迴圈
                foreach (DataRow theRow in ds.Tables["dsProducts"].Rows)
                {
                    Response.Write(theRow[0].ToString() + "," + theRow["ProductName"].ToString() + "<br>");
                }

2013年6月14日 星期五

bindingNavigator 調整高度(圖片大小)

bindingNavigator 調整高度(圖片大小)

先要把bindingNavigator的 AutoSize 屬性,改為 False

才能調整高度

圖片大小,要再調整  ImageScalingSize ,才能調整大小

2013年6月13日 星期四

清空form上所有textbox的值


清空form上所有textbox的值

// 如果textbox沒放在其他容器內的話
foreach (Control obj in this.Controls)
            {
                if (obj.GetType() == typeof(TextBox))
                {
                    ((TextBox)obj).Text = "";
                }
            }

//如果有放在其他容器內的話
private void button1_Click(object sender, EventArgs e)
        {
         
            foreach (Control obj in this.Controls)
            {
                test(obj);
            }

        }



        private void test(Control controls)
        {
            if (controls.HasChildren)
            {
                foreach (Control control in controls.Controls)
                {
                    test(control);
                }
            }
            else
            {
                if (controls.GetType() == typeof(TextBox))
                {
                    ((TextBox)controls).Text = "";
                }
            }
        }

有母版的,但不一定是最好的解法,有查到更好的再更新 2015-03-05
foreach (Control obj in this.Master.FindControl("ContentPlaceHolder1").Controls)
                    {

                       if (obj.GetType() == typeof(Label))
                        {
                            if (((Label)obj).ID.ToString().Substring(0,12) == "lblmeetingmsg")
                            {
                                ((Label)obj).Text = "";
                            }

                        }

                    }

Master加了 Panel1就不行了,後來改成這樣就好了,好像用Panel1的方式比較好點

foreach (Control obj in this.Panel1.Controls)
                    {
                     if (obj.GetType() == typeof(Label))
                        {
                            if (((Label)obj).ID.ToString().Length > 12)
                            {
                                ((Label)obj).Text = "";
                            }

                        }

                        if (obj.GetType() == typeof(CheckBox))
                        {
                            ((CheckBox)obj).Checked = false;
                        }

                    }

前台 js
<script language="javascript" type="text/javascript">
function ClearAllTextBox() {
 var obj = window.document.forms[0];
 for (i = 0; i < obj.elements.length; i++) {
 var elem = obj.elements[i];
 if (elem) {
 if (elem.type == "text") {
 elem.value = "";
 }
 } 
 } 
 }
 </script>