将新记录插入SQL Server

时间:2015-11-26 08:30:59

标签: c# sql sql-server visual-studio-2012 adventureworks

我仍然是新手,甚至在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 = "";
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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);