2015-11-26 166 views
2

我在這裏甚至在Visual Studio中都還是新手,所以我不完全知道Visual Studio中的所有功能。我試圖將數據插入到SQL Server中。當我運行這些代碼並輸入必要的信息時,我的標籤消息就會顯示出來,但是當我查看SQL Server時,沒有任何變化。 Visual Studio已連接到SQL Server。我在這裏先向您的幫助表示感謝。在SQL Server中插入新記錄

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

public partial class NewEmployee : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Page.IsPostBack == true) 
     { 
      Label14.Text = ("**Your data has been entered in SQL"); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection vid = new SqlConnection("Data Source=JOSAN;Initial Catalog=AdventureWorks2012;Integrated Security=True"); 
     { 
      SqlCommand xp = new SqlCommand("Insert into HumanResources.Employee(NationalIDNumber, LoginID, JobTitle, BirthDate, MaritalStatus, Gender, HireDate, SalariedFlag, VacationHours, SickLeaveHours, CurrentFlag, ModifiedDate) Values(@NationalIDNumber, @loginID, @JobTitle, @BirthDate, @MaritalStatus, @Gender, @HireDate, @SalariedFlag, @VacationHours, @SickLeaveHours, @CurrentFlag, @ModifiedDate)", vid); 
      xp.Parameters.AddWithValue("@NationalIDNumber", Convert.ToInt32(TextBox1.Text)); 
      xp.Parameters.AddWithValue("@LoginID", TextBox2.Text); 
      xp.Parameters.AddWithValue("@JobTitle", TextBox3.Text); 
      xp.Parameters.AddWithValue("@BirthDate", Convert.ToDateTime(TextBox4.Text)); 
      xp.Parameters.AddWithValue("@MaritalStatus", Convert.ToChar(TextBox5.Text)); 
      xp.Parameters.AddWithValue("@Gender", Convert.ToChar(TextBox6.Text)); 
      xp.Parameters.AddWithValue("@HireDate", Convert.ToDateTime(TextBox7.Text)); 
      xp.Parameters.AddWithValue("@SalariedFlag", Convert.ToInt32(TextBox8.Text)); 
      xp.Parameters.AddWithValue("@VacationHours", Convert.ToInt32(TextBox9.Text)); 
      xp.Parameters.AddWithValue("@SickLeaveHours", Convert.ToInt32(TextBox10.Text)); 
      xp.Parameters.AddWithValue("@CurrentFlag", Convert.ToInt32(TextBox11.Text)); 
      xp.Parameters.AddWithValue("@ModifiedDate", Convert.ToDateTime(TextBox12.Text)); 

      try 
      { 
       vid.Open(); 
       xp.ExecuteNonQuery(); 
      } 
      catch 
      { 
       vid.Close(); 
      } 

      if (IsPostBack) 
      { 
       TextBox1.Text = ""; 
       TextBox2.Text = ""; 
       TextBox3.Text = ""; 
       TextBox4.Text = ""; 
       TextBox5.Text = ""; 
       TextBox6.Text = ""; 
       TextBox7.Text = ""; 
       TextBox8.Text = ""; 
       TextBox9.Text = ""; 
       TextBox10.Text = ""; 
       TextBox11.Text = ""; 
       TextBox12.Text = ""; 
      } 
     } 
    } 
} 
+0

是SQL服務器上的命令?你的列類型到底是什麼?如何嘗試使用'Add'方法添加參數並指定它的類型和大小? ['AddWihValue' _might_有時會產生意外的結果](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)並使用'using'語句自動處理連接和命令,而不是手動調用'Close'方法。 –

+0

您是否嘗試過通過ssms或sqlcmd運行查詢?它是否工作,然後,什麼都不做或給出錯誤信息? – BeanFrog

+2

'1.使用事務和異常處理''2。使用Sql Profiler來跟蹤發送給SQL Server的值。 –

回答

0

試試這個

using(SqlConnection vid = new SqlConnection("Data Source=JOSAN;Initial Catalog=AdventureWorks2012;Integrated Security=True")) 
{ 
    using(SqlCommand xp = new SqlCommand()) 
    { 
     xp.CommandText = @"Insert into HumanResources.Employee(NationalIDNumber, LoginID, JobTitle, BirthDate, MaritalStatus, Gender, HireDate, SalariedFlag, VacationHours, SickLeaveHours, CurrentFlag, ModifiedDate) Values(@NationalIDNumber, @loginID, @JobTitle, @BirthDate, @MaritalStatus, @Gender, @HireDate, @SalariedFlag, @VacationHours, @SickLeaveHours, @CurrentFlag, @ModifiedDate)"; 
     xp.Connection = vid; 
    xp.Parameters.AddWithValue("@NationalIDNumber", TextBox1.Text); 
    xp.Parameters.AddWithValue("@LoginID", TextBox2.Text); 
    xp.Parameters.AddWithValue("@JobTitle", TextBox3.Text); 
    xp.Parameters.AddWithValue("@BirthDate", TextBox4.Text); 
    xp.Parameters.AddWithValue("@MaritalStatus", Convert.ToChar(TextBox5.Text); 
    xp.Parameters.AddWithValue("@Gender", TextBox6.Text); 
    xp.Parameters.AddWithValue("@HireDate", TextBox7.Text); 
    xp.Parameters.AddWithValue("@SalariedFlag", TextBox8.Text); 
    xp.Parameters.AddWithValue("@VacationHours", TextBox9.Text); 
    xp.Parameters.AddWithValue("@SickLeaveHours", TextBox10.Text); 
    xp.Parameters.AddWithValue("@CurrentFlag", TextBox11.Text); 
    xp.Parameters.AddWithValue("@ModifiedDate", TextBox12.Text); 
     try 
     { 
      vid.Open(); 
      xp.ExecuteNonQuery(); 
     } 
     catch 
     { 
      MessgeBox.Show(e.Message.ToString(), "Error Message"); 
      vid.Close(); 
     } 
     if (IsPostBack) 
     { 
      TextBox1.Text = ""; 
      TextBox2.Text = ""; 
      TextBox3.Text = ""; 
      TextBox4.Text = ""; 
      TextBox5.Text = ""; 
      TextBox6.Text = ""; 
      TextBox7.Text = ""; 
      TextBox8.Text = ""; 
      TextBox9.Text = ""; 
      TextBox10.Text = ""; 
      TextBox11.Text = ""; 
      TextBox12.Text = ""; 
     } 
    } 
} 

編輯

您可以使用隱式轉換AddWithValue並更改聲明這樣

xp.Parameters.AddWithValue("@NationalIDNumber", TextBox1.Text); 
xp.Parameters.AddWithValue("@LoginID", TextBox2.Text); 
xp.Parameters.AddWithValue("@JobTitle", TextBox3.Text); 
xp.Parameters.AddWithValue("@BirthDate", TextBox4.Text); 
xp.Parameters.AddWithValue("@MaritalStatus", Convert.ToChar(TextBox5.Text); 
xp.Parameters.AddWithValue("@Gender", TextBox6.Text); 
xp.Parameters.AddWithValue("@HireDate", TextBox7.Text); 
xp.Parameters.AddWithValue("@SalariedFlag", TextBox8.Text); 
xp.Parameters.AddWithValue("@VacationHours", TextBox9.Text); 
xp.Parameters.AddWithValue("@SickLeaveHours", TextBox10.Text); 
xp.Parameters.AddWithValue("@CurrentFlag", TextBox11.Text); 
xp.Parameters.AddWithValue("@ModifiedDate", TextBox12.Text); 
+1

爲什麼你認爲在命令上使用逐字串會改變什麼?它不會包含任何轉義字符。 –

+0

@SonerGönül:謝謝你的通知。讓我改變它 –

相關問題