2015年11月4日 星期三

避免相同資料 重複輸入(重複新增)


http://www.dotblogs.com.tw/mis2000lab/archive/2009/10/09/10980.aspx

'----自己寫的----
Imports System
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient
'----自己寫的----
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Conn As SqlConnection = New SqlConnection
        Conn.ConnectionString = WebConfigurationManager.ConnectionStrings("Web.Config檔案裡面 ConnectionString").ConnectionString
        Conn.Open() '---- 連結DB

        Dim dr As SqlDataReader = Nothing
        Dim cmd As SqlCommand
        cmd = New SqlCommand("select * from User資料表 where 帳號 = '" & Trim(TextBox2.Text) "'", Conn)

使用參數的寫法,可以避免一些攻擊:
cmd = New SqlCommand("select * from User資料表 where 帳號 = @id", Conn)
cmd.Parameters.AddWithValue("@id", TextBox2.Text)

        dr = cmd.ExecuteReader() '---- 執行SQL指令,取出資料
        '////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        If dr.HasRows() Then

            Response.Write("Error~ 找到相同的帳號,所以程式停止!!")
            Response.End()
        Else '-- 沒有找到相同標題的文章,所以可以新增這筆資料 '-- 請自己撰寫資料新增的程式碼
            Response.Write("新增資料成功!!")
        End If

        If Not (dr Is Nothing) Then
            cmd.Cancel()
            dr.Close()
        End If
        '---- Close the connection when done with it.
        If (Conn.State = ConnectionState.Open) Then
            Conn.Close()
            Conn.Dispose()
        End If
    End Sub






2015年11月3日 星期二

如何產生隨機不重覆的數字

方法一
http://www.allenkuo.com/EBook5/view.aspx?TreeNodeID=72&id=1423

//static void Main(string[] args)
        {
        var hs = new HashSet();
        int counter = 1;
        int con = int.Parse(TextBox1.Text);
        //int con = 10;
        var rnd = new Random();
        while (hs.Count < con)
        {
           counter++;
                hs.Add(rnd.Next(con)+1);
            }

        //Console.WriteLine("一共取了" + counter + "亂數才完成");

        //Console.WriteLine("結果如下:");
        string sum = "";
            foreach (int item in hs){
              //Console.Write(item.ToString() + ", ");

                sum = sum + item.ToString() + ", ";
            }
            Label1.Text = sum;
            Label2.Text = "一共取了" + counter + "次亂數才完成";
        Console.WriteLine("");
        }

方法二
http://blog.yljh.mlc.edu.tw/blog/blog_form.asp?blog=paguma&view=writings&writing_id=301

Option Base 1
  /* Option Base 是指定陣列註標值開始的指令
  /* Option Base 1 表示陣列元素從 1 開始
  /* Option Base 後方只能接 0 或 1

Dim I, K, X, Temp, A(42) As Integer
  /*宣告所需使用到的變數及陣列
  /* A(42)包含的元素有 A(1)、A(2)....A(42) 

For K = 1 To 42
 A(K) = K
Next K

  /* 利用 For 迴圈將數值依序存入陣列
  /* 當 K = 1 時,A(1) = 1
  /* 當 K = 2 時,A(2) = 2
  /* ................. 
  /* 當 K = 42時,A(42) = 42

Randomize
  /* 使用Rnd函數時,務必在使用前加上 Randomize指令
  /*Randomize為啟動亂數種子,後方可加整數 -32768 ~ 32768
  /*功能是讓 Rnd()函數產生的亂數更不規則
  /*若沒有加上此指令時,每次執行產生的亂數將會固定

For I = 1 To 6
 X = Int(Rnd()*(42-I+1))+1
 Temp = A(X)
 A(X) = A(42-I+1)
 A(42-I+1) = Temp
Print A(42-I+1) & " ";
Next I

  /* For I = 1 To 6 代表要執行 6 次動作
  /* X 是代表每次隨機選取的值,原來應是 X=INT(Rnd()*42)+1
  /* 因為每次選取的範圍要遞減
  /* 第1次1~42選1個,第2次1~41個選1個,第3次...
  /* 也就是第1次為 Rnd*42,第2次為 Rnd*41,第3次...
  /* 我們利用 I 值每回合都會加 1 的特性加到程式碼
  /* Rnd()*42 調整為 Rnd()*(42-I+1)
  /* 重覆6次,每次 X 值取亂數的範圍都會少1
  /*
  /* 再來是將隨機選取的位置與最後位置的數值做交換
  /* 每個回合最後的位置並不是固定在 A(42)
  /* 第1次為 A(42),第2次為 A(41),第3次...
  /* 每回合最後的位置配合 I 值調整為 A(42-I+1)
  /* 底下為將數值互換的程式
  /* Temp=A(X) : A(X)=A(42-I+1) : A(42-I+1)=Temp
  /*
  /* 最後是每個回合都將取得的數值印出
  /* 因為是放在每回合最後的位置,因此是 Print A(42-I+1)

C# Int String 轉換



String轉換1. ToString()方法   2.System.Convert.ToString()

int轉換1. int.Parse(xxxxxxxxxxxxxx)    2.System.Convert.ToIntXX()

https://msdn.microsoft.com/zh-tw/library/bb397679.aspx
http://kuomingwang.blogspot.tw/2009/03/c-int-string.html



TextBox限制只能輸入數字
http://adamagod.blogspot.tw/2012/10/c-net-textbox.html

在前端加入Script如下

然後再後端把想要限制的TextBox在他的Attribute加入一個OnKeyPress的事件會去呼叫上面的javascript即可!

txtOnlyNumber.Attributes.Add("OnKeyPress", "txtKeyNumber();");