星期二, 8月 08, 2017

C# NET Insert Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Transaction : System.Web.UI.Page
{
    string msg = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Insert();
    }
    protected void Insert()
    {

        if (checkInput())
        {

             SqlConnection conn = new SqlConnection("Data Source=K1DB01;Initial Catalog=KINSUS_02DB;User ID=notesadmin;Password=notes@kinsus");
                                 
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand("Insert Into [KINSUS_02DB].[dbo].[Transaction](ID,Name,Type,Price,Qty,Reason) values(@paramID,@paramName,@paramType,@paramPrice,@paramQty,@paramReason)", conn, tran);
                try
                {  
           
                    cmd.Parameters.Add("@paramID", SqlDbType.NChar, 10).Value = TextID.Text;
       
                cmd.Parameters.Add("@paramName", SqlDbType.NVarChar, 50).Value = TextName.Text;
       
                cmd.Parameters.Add("@paramType", SqlDbType.NChar, 10).Value = DropDownListType.Text;
       
                cmd.Parameters.Add("@paramPrice", SqlDbType.Money).Value = TextPrice.Text;
        cmd.Parameters.Add("@paramQty", SqlDbType.Int).Value = TextQty.Text;
        cmd.Parameters.Add("@paramReason", SqlDbType.NVarChar).Value = TextReason.Text;
        LabelMsg.Text = cmd.CommandText;
        int rows = cmd.ExecuteNonQuery();
        if (rows > 0)
        {
            tran.Commit();
            LabelMsg.Text = string.Format("已成功寫入{0}筆",rows);
        }
       
        }
        catch (Exception ex)
        {
            tran.Rollback();
            Response.Write(ex.Message);
            LabelMsg.Text = cmd.CommandText;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            TextID.Text = "";
            TextPrice.Text = "";
            TextQty.Text = "";
            //ButtonFill_Click();
        }

     
        }
         else
        {
            LabelMsg.Text = msg;
        }
    }
    protected bool checkInput()
    {
        bool ValidStatus = true;
        if (string.IsNullOrEmpty(TextID.Text))
        {
            ValidStatus = false;
            msg = "代號不得為空白<BR/>";
        }

       // if (string.IsNullOrEmpty(TextPrice.Text))
        float f;
            if (!float.TryParse(TextPrice.Text,out f))
        {
            ValidStatus = false;
            msg += "價格不得為空白,且必須為數字<BR/>";
        }
        int i;

        if (!int.TryParse(TextQty.Text,out i))
        {
            ValidStatus = false;
            msg += "數量不得為空白,且必須為數字<BR/>";
        }
        if (string.IsNullOrEmpty(DropDownListType.Text))
        {
            ValidStatus = false;
            msg += "類型不得為空白<BR/>";
        }
        return ValidStatus;
    }
    protected void ButtonFill_Click(object sender, EventArgs e)
    {
        string connString = WebConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from [KINSUS_02DB].[dbo].[Transaction]",conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Trans");
        conn.Close();
        conn.Dispose();
        gvTrans.DataSource = ds;
        gvTrans.DataBind();
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        DataTable dt = new DataTable("Remind");
        dt.Columns.Add(new DataColumn("Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Principle", typeof(string)));
        dt.Columns.Add(new DataColumn("Priority", typeof(string)));

        DataRow row1 = dt.NewRow();
        row1["Type"] = "資金控管";
        row1["Principle"] = "每檔投入比例不得超過25%";
        row1["Priority"] = "Very High";
        dt.Rows.Add(row1);
        DataRow row2 = dt.NewRow();
        row2["Type"] = "資金控管";
        row2["Principle"] = "至少留30%現金應急";
        row2["Priority"] = "Very High";
        dt.Rows.Add(row2);
        DataRow row3 = dt.NewRow();
        row3["Type"] = "八字真言";
        row3["Principle"] = "長期穩定獲利一致";
        row3["Priority"] = "High";
        dt.Rows.Add(row3);
        DataRow row4 = dt.NewRow();
        row4["Type"] = "價值投資";
        row4["Principle"] = "獲利安全評價成長";
        row4["Priority"] = "High";
        dt.Rows.Add(row4);
        DataRow row5 = dt.NewRow();
        row5["Type"] = "獲利";
        row5["Principle"] = "ROE > 15%";
        row5["Priority"] = "High";
        dt.Rows.Add(row5);
        DataRow row6 = dt.NewRow();
        row6["Type"] = "安全";
        row6["Principle"] = "自由現金流為正";
        row6["Priority"] = "High";
        dt.Rows.Add(row6);
        DataRow row7 = dt.NewRow();
        row7["Type"] = "評價";
        row7["Principle"] = "本益成長比<0.7 股利折現評價>15%";
        row7["Priority"] = "High";
        dt.Rows.Add(row7);
        DataRow row8 = dt.NewRow();
        row8["Type"] = "成長";
        row8["Principle"] = "獲利趨勢向上,毛利不可降";
        row8["Priority"] = "Normal";
        dt.Rows.Add(row8);
        gvTrans.DataSource = dt;
        gvTrans.DataBind();

    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {

    }
}

沒有留言: