gpt4 book ai didi

c# - 无法使用 C# visual studio 2008 将数据插入表中

转载 作者:行者123 更新时间:2023-12-03 21:50:18 27 4
gpt4 key购买 nike

你好每次我都试图创建简单的学生表格 n 将数据添加到数据库但是不能这样做,程序执行期间没有错误,应用程序执行得很好但表中没有变化,n 值是nt 插入到表中,问题可能是什么 plz 建议我,我在下面写我的代码..提前谢谢 :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace School.Project
{

class Student
{
public static string constr = System.Configuration.ConfigurationManager.ConnectionStrings"SchoolConnectionString"].ConnectionString;

public static int AddStudent(string title, string fname, string lname, string fathername, string gender, string clas, string sec, int age, string dob, string religion, string caste, string image, string address, string homeno, string cell, string email)
{
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlTransaction t = con.BeginTransaction();

SqlCommand cmd = new SqlCommand("INSERT INTO Student (Title,FirstName,LastName,FatherName,Gender,Class,Section,Age,DateOfBirth,Religion,Caste,Image,Address,HomePhone,CellPhone,Email) VALUES(@Title,@FirstName,@LastName,@FatherName,@Gender,@Class,@Section,@Age,@DateOFBirth,@Religion,@Caste,@Image,@Address,@HomePhone,@CellPhone,@Email)",con);
cmd.Parameters.AddWithValue("@Title", title);
cmd.Parameters.AddWithValue("FirstName", fname);
cmd.Parameters.AddWithValue("LastName", lname);
cmd.Parameters.AddWithValue("@FatherName", fathername);
cmd.Parameters.AddWithValue("@Gender", gender);
cmd.Parameters.AddWithValue("@Class", clas);
cmd.Parameters.AddWithValue("@Section", sec);
cmd.Parameters.AddWithValue("Age", age);
cmd.Parameters.AddWithValue("DateOfBirth", dob);
cmd.Parameters.AddWithValue("@Religion", religion);
cmd.Parameters.AddWithValue("Caste", caste);
cmd.Parameters.AddWithValue("Image", image);
cmd.Parameters.AddWithValue("Address", address);
cmd.Parameters.AddWithValue("@HomePhone", homeno);
cmd.Parameters.AddWithValue("@CellPhone", cell);
cmd.Parameters.AddWithValue("@Email", email);
cmd.Transaction = t;

int i = cmd.ExecuteNonQuery();
t.Commit();
con.Close();
return i;
}

private void btSave_Click(object sender, EventArgs e)
{
string title = cbTitle.SelectedItem.ToString();
string fname = txtfname.Text;
string lname = txtlname.Text;
string fathername = txtfatherName.Text;
string gender = cbGender.SelectedItem.ToString();
string clas = cbClass.SelectedItem.ToString();
string section = cbSection.SelectedItem.ToString();
int age = int.Parse(txtAge.Text);
string dob = txtdob.Text;
string religion = txtReligion.Text;
string caste = txtCaste.Text;
string imagepath = txtpath.Text;
string address = txtAddress.Text;
string homeno = txtHome.Text;
string cell = txtCell.Text;
string email = txtEMail.Text;

int i = Project.Student.AddStudent(title, fname, lname, fathername, gender, clas, section, age, dob, religion, caste, imagepath, address, homeno, cell, email);
if (i == 1)
{
MessageBox.Show("Student Added Succesfully, Thanq", "Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearAll();
}
else
{
MessageBox.Show("Couldnt enter the data", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

最佳答案

在查询的某些字段上添加括号 [ ],因为其中一些是保留字:

SqlCommand cmd = new SqlCommand("
INSERT INTO Student ([Title],FirstName,LastName,
FatherName,Gender,
[Class],Section,Age,DateOfBirth,
Religion,Caste,[Image],Address,
HomePhone,CellPhone,Email)
VALUES(@Title,@FirstName,@LastName,
@FatherName,@Gender,@Class,@Section,
@Age,@DateOFBirth,@Religion,@Caste,
@Image,@Address,@HomePhone,
@CellPhone,@Email)",con);

[Title]

[Image]

更新 1

代替 SqlTransaction t = con.BeginTransaction();

试试这个:

SqlTransaction t = con.BeginTransaction(IsolationLevel.ReadCommitted)

Source: Use database transactions

关于c# - 无法使用 C# visual studio 2008 将数据插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9891873/

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