我在这里甚至在 Visual Studio 中还是新手,所以我并不完全了解 Visual Studio 中的所有功能。我正在尝试将数据插入 SQL Server。当我运行此代码并输入必要的信息时,我的标签消息会显示出来,但是当我查看 SQL Server 时,什么都没有改变。Visual Studio 已连接到 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 = "";
}
}
}
}