gpt4 book ai didi

c# - 将新记录插入 SQL Server

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:27 24 4
gpt4 key购买 nike

我在这里甚至在 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 = "";
}
}
}
}

最佳答案

试试这个

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);

关于c# - 将新记录插入 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com