protected void BtnWrite_Click(object sender, EventArgs e)
        {
            Memos memos = new Memos();

            memos.Name     = TxtName.Text;
            memos.Email    = TxtEmail.Text;
            memos.Title    = TxtTitle.Text;
            memos.PostDate = DateTime.Now;
            memos.PostIP   = Request.UserHostAddress;

            var connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

            using (var conn = new SqlConnection(connString))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }

                try
                {
                    // 일반 쿼리 사용

                    /*var query = @"Insert Memos(Name, Email, Title, PostDate, PostIP)
                     *                Values(@Name, @Email, @Title, @PostDate, @PostIP)";
                     * SqlCommand cmd = new SqlCommand(query, conn);
                     * cmd.CommandType = System.Data.CommandType.Text;*/

                    // 저장프로시저 사용
                    SqlCommand cmd = new SqlCommand("WriteMemo", conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Name", memos.Name);
                    cmd.Parameters.AddWithValue("@Email", memos.Email);
                    cmd.Parameters.AddWithValue("@Title", memos.Title);
                    cmd.Parameters.AddWithValue("@PostDate", memos.PostDate);
                    cmd.Parameters.AddWithValue("@PostIP", memos.PostIP);

                    cmd.ExecuteNonQuery();

                    LblResult.Text = "저장되었습니다";
                }
                catch (Exception ex)
                {
                    LblResult.Text = $"예외발생 : {ex}";
                }
            }
        }
Ejemplo n.º 2
0
        protected void BtnWrite_Click(object sender, EventArgs e)
        {
            Memos memos = new Memos();

            memos.Name     = TxtName.Text;
            memos.Email    = TxtEmail.Text;
            memos.Title    = TxtTitle.Text;
            memos.PostDate = DateTime.Now;
            memos.PostIP   = Request.UserHostAddress;

            var connstring = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

            using (var conn = new SqlConnection(connstring))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }

                try
                {
                    SqlCommand cmd = new SqlCommand("WriteMemo", conn); // 저장프로시져
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Name", memos.Name);
                    cmd.Parameters.AddWithValue("@Email", memos.Email);
                    cmd.Parameters.AddWithValue("@Title", memos.Title);
                    cmd.Parameters.AddWithValue("@PostDate", memos.PostDate);
                    cmd.Parameters.AddWithValue("@PostIP", memos.PostIP);

                    cmd.ExecuteNonQuery();

                    lblDisplay.Text = "저장되었습니다!";
                }
                catch (Exception ex)
                {
                    lblDisplay.Text = $"예외발생 {ex}";
                }
            }
        }