你好,每当我尝试创建简单的学生表格n都将数据添加到数据库中,但要这样做时,程序执行过程中没有错误,应用程序执行得很好,但表中没有任何变化,n值已插入nt在桌子上,问题可能会提示我什么,我在下面写了我的代码。
提前thanq :)
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);
[标题]
[图片]
更新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/