iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 8
0
自我挑戰組

What a good thing we lose? What a bad thing we knew?系列 第 8

【Day 8】 在Visual Studio 2017 寫一個簡單的問卷(1/2)

大家好,大家應該都有在網路上填過問卷,今天跟大家一起學習,如何建立超簡單的問卷。
因為這次內容有點多,所以分成兩次講解。

Step 1. 建立資料表

CREATE TABLE [dbo].[Survey] (
    [Id]      INT            NOT NULL,
    [name]    NVARCHAR (50)  NULL,
    [q1]      NVARCHAR (200) NULL,
    [q2]      NCHAR (1)      NULL,
    [q3]      NCHAR (1)      NULL,
    [q4]      NCHAR (1)      NULL,    
    [send_dt] DATETIME       NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

Step 2. 在Model建立class
加入 using System.ComponentModel.DataAnnotations;

 public class survey
{
    [Key]
    [Display(Name = "序號")]
    public string id { get; set; }

    [Display(Name = "姓名")]
    public string name { get; set; }


    [Required]
    [StringLength(200, ErrorMessage = "{0} 的長度至少必須為 {2} 個字元。", MinimumLength = 20)]
    [Display(Name = "請寫出您對本次課程的心得(20字以上)")]
    public string q1 { get; set; }

    [Required]
    [Display(Name = "1. 您對本次課程主題安排滿意嗎?")]
    public string q2 { get; set; }

    [Required]
    [Display(Name = "2.您對本次課程內容滿意嗎?")]
    public string q3 { get; set; }

    [Required]
    [Display(Name = "3.您對本次安排的飲食滿意嗎?")]
    public string q4 { get; set; }

   
}

Step 3. 建立儲存資料的Function

    public int Save_senddata(string name ,string q1, string q2, string q3, string q4)
    {

        using (SqlConnection conn = new SqlConnection(strConnString))
        {
            conn.Open();

            SqlCommand scom = new SqlCommand("", conn);

            scom.CommandText = @"
            	                insert into [dbo].[Survey]
            			                                    (	
            													name,
                                                                q1,
                                                                q2,
                                                                q3,
                                                                q4,
                                                                send_dt
            												                                                                   
                                                            )
            			                                    values      
            			                                    
            			                                    (	
            		    										@name,
                                                                @q1,
                                                                @q2,
                                                                @q3,
                                                                @q4,
                                                                getDate()
                                                              
            			                                    ) 
                                                            
                                                             ";



            scom.Parameters.AddWithValue("@name", name);
            scom.Parameters.AddWithValue("@q1", q1);
            scom.Parameters.AddWithValue("@q2", q2);
            scom.Parameters.AddWithValue("@q3", q3);
            scom.Parameters.AddWithValue("@q4", q4);
         
            int i = scom.ExecuteNonQuery();
            scom.Dispose();
            scom.Clone();
            return i;
        }
    }
    #endregion

上一篇
【Day 7】 隱藏在黑暗之中的網路-暗網
下一篇
【Day 9】 在Visual Studio 2017 寫一個簡單的問卷(2/2)
系列文
What a good thing we lose? What a bad thing we knew?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言