我在這裏甚至在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 = "";
}
}
}
}
是SQL服務器上的命令?你的列類型到底是什麼?如何嘗試使用'Add'方法添加參數並指定它的類型和大小? ['AddWihValue' _might_有時會產生意外的結果](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)並使用'using'語句自動處理連接和命令,而不是手動調用'Close'方法。 –
您是否嘗試過通過ssms或sqlcmd運行查詢?它是否工作,然後,什麼都不做或給出錯誤信息? – BeanFrog
'1.使用事務和異常處理''2。使用Sql Profiler來跟蹤發送給SQL Server的值。 –