伊莉討論區

標題: insert into MSSQL [打印本頁]

作者: hildaz    時間: 2017-3-30 08:24 PM     標題: insert into MSSQL

我程式點下去 有值可是沒有存寫資料庫的動作阿~~~~= =!!
請大大幫忙看哪要修一下嗎??

protected void insert_Click(object sender, ImageClickEventArgs e)
    {
        Literal myMsg = new Literal();
        string Sex = null;
        TextBox33.Text = RadioButtonList3.SelectedIndex.ToString().Trim();
        TextBox34.Text = RadioButtonList2.SelectedIndex.ToString().Trim();
        TextBox35.Text = RadioButtonList1.SelectedIndex.ToString().Trim();
        if (CheckBox1.Checked) { Sex = " 男"; }
        if (CheckBox2.Checked) { Sex = " 女"; }
        string strConn =  ConfigurationManager.ConnectionStrings["lseformsConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(strConn);
        myConn.Open();
        string CommandText="INSERT INTO [cancer_plan] ([date], [creator], [pat_no], [pat_name], [pat_birth], [pat_sex], [pat_room], [pat_bed]"
        + ", [timetable],[diagnosis], [history], [Comorbitidy], [assess], [meeting], [meetingdate],[clinical_CT],[clinical_N],[clinical_M]"
        + ", [clinical_SG], [pathology_PT], [pathology_N], [pathology_M], [pathology_SG], [other_stage], [ECOG], [BS], [Chemotherapy],"
        + ",[Regimens], [Courses], [Radiotherapy], [location], [dose], [Surger_plan], [regimens_plan],[dose_plan], [other_plan],[suggestion])"
        + " VALUES (@date, @creator, @pat_no, @pat_name, @pat_birth, @pat_sex, @pat_room, @pat_bed, @timetable, @diagnosis,"
        + "@history, @Comorbitidy, @assess, @meeting, @meetingdate, @clinical_CT, @clinical_N, @clinical_M, @clinical_SG,"
        + "@pathology_PT, @pathology_N, @pathology_M,@pathology_SG, @other_stage, @ECOG, @BS, @Chemotherapy, @Regimens, @Courses, "
        + "@Radiotherapy, @location, @dose, @Surger_plan, @regimens_plan, @dose_plan,@other_plan, @suggestion)";
        System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand(CommandText, myConn);
        myCommand.Parameters.AddWithValue(@"date", today.Text);
        myCommand.Parameters.AddWithValue(@"creator", UsrName.Text);
        myCommand.Parameters.AddWithValue(@"pat_name", TextBox1.Text);
        myCommand.Parameters.AddWithValue(@"pat_room", TextBox2.Text);
        myCommand.Parameters.AddWithValue(@"pat_no", TextBox3.Text);
        myCommand.Parameters.AddWithValue(@"pat_bed", TextBox4.Text);
        myCommand.Parameters.AddWithValue(@"pat_sex", Sex);
        myCommand.Parameters.AddWithValue(@"pat_birth", TextBox6.Text);
        myCommand.Parameters.AddWithValue(@"meetingdate", TextBox7.Text);
        myCommand.Parameters.AddWithValue(@"clinical_CT", TextBox8.Text);
        myCommand.Parameters.AddWithValue(@"clinical_N", TextBox9.Text);
        myCommand.Parameters.AddWithValue(@"clinical_M", TextBox10.Text);
        myCommand.Parameters.AddWithValue(@"clinical_SG", TextBox11.Text);
        myCommand.Parameters.AddWithValue(@"pathology_PT", TextBox12.Text);
        myCommand.Parameters.AddWithValue(@"pathology_N", TextBox13.Text);
        myCommand.Parameters.AddWithValue(@"pathology_M", TextBox14.Text);
        myCommand.Parameters.AddWithValue(@"pathology_SG", TextBox15.Text);
        myCommand.Parameters.AddWithValue(@"chemotherapy", TextBox16.Text);
        myCommand.Parameters.AddWithValue(@"regimens", TextBox17.Text);
        myCommand.Parameters.AddWithValue(@"courses", TextBox18.Text);
        myCommand.Parameters.AddWithValue(@"radiotherapy", TextBox19.Text);
        myCommand.Parameters.AddWithValue(@"location", TextBox20.Text);
        myCommand.Parameters.AddWithValue(@"dose", TextBox21.Text);
        myCommand.Parameters.AddWithValue(@"suggestion", TextBox22.Text);
        myCommand.Parameters.AddWithValue(@"diagnosis", TextBox23.Text);
        myCommand.Parameters.AddWithValue(@"history", TextBox24.Text);
        myCommand.Parameters.AddWithValue(@"comorbitidy", TextBox25.Text);
        myCommand.Parameters.AddWithValue(@"assess", TextBox26.Text);
        myCommand.Parameters.AddWithValue(@"other_stage", TextBox27.Text);
        myCommand.Parameters.AddWithValue(@"surger_plan", TextBox28.Text);
        myCommand.Parameters.AddWithValue(@"regimens_plan", TextBox29.Text);
        myCommand.Parameters.AddWithValue(@"dose_plan", TextBox30.Text);
        myCommand.Parameters.AddWithValue(@"other_plan", TextBox31.Text);
        myCommand.Parameters.AddWithValue(@"BS", TextBox32.Text);
        myCommand.Parameters.AddWithValue(@"timetable", TextBox33.Text);
        myCommand.Parameters.AddWithValue(@"meeting", TextBox34.Text);
        myCommand.Parameters.AddWithValue(@"ECOG", TextBox35.Text);

        myConn.Close();
        myMsg.Text = "<script>alert('存檔成功!')</script><br>";<<當然也不可能跳出提示
    }


作者: Josie_2016    時間: 2017-4-1 11:59 AM

在myConn.Close();之前要執行==>myCommand.ExecuteNonQuery();

執行Javascript用Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "<script>alert('存檔成功!')</script>");
作者: smallanan    時間: 2017-6-5 01:43 AM

Josie_2016大大說的沒錯

樓主你的code只有說我要insert跟相關參數(如同計畫書),但是沒叫程式去執行阿,當然沒有值進去阿
作者: bwong42!@#    時間: 2017-6-10 09:00 AM

提示: 作者被禁止或刪除 內容自動屏蔽
作者: troy007    時間: 2017-7-24 09:32 PM

不肯定這樣寫會不會錯︰
myCommand.Parameters.AddWithValue(@"ECOG", TextBox35.Text);
我自己是這樣寫的︰
myCommand.Parameters.AddWithValue("@ECOG", TextBox35.Text);

還有加完Parameters後要執行才能寫入SQL Server︰
myCommand.ExecuteNonQuery();

另外,整段最好用Try Catch包住,myConn.Close()放在Finally內
這樣發生存取問題也不會導致程式失敗和釋放資源
作者: ggnnyy    時間: 2017-10-12 10:39 AM

troy007 發表於 2017-7-24 09:32 PM
不肯定這樣寫會不會錯︰
myCommand.Parameters.AddWithValue(@"ECOG", TextBox35.Text);
我自己是這樣寫的 ...

我習慣整段用using(){}這樣包住,這樣說也可以自動釋放資源,不知道這兩種方式有沒有好壞之分...




歡迎光臨 伊莉討論區 (http://www99.eyny.com/) Powered by Discuz!