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

本文关键字:数据 插入 2008 studio C#visual | 更新日期: 2023-09-27 18:29:12

hello every我试图创建简单的学生表单n将数据添加到数据库中,但无法做到这一点,在程序执行过程中没有错误,应用程序执行得很好,但表中没有更改,n值没有插入表中,plz建议我怎么做,我在下面写代码。。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);
        }
    }

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

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

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)

来源:使用数据库事务

有些参数包含"@"符号,有些则不包含。。。。

1.)不要传递太多参数。你想增加一个学生,这应该会让你大吃一惊。只传递一个参数-用所需值填充的类Student。

2.)我认为这里没有必要进行交易。你只想推一个对象,所以如果它失败了,结果是一样的——没有做任何更改。

3.)正如Daren所说,您的查询中没有正确写入参数

编辑:只是试了一下简化版,效果很好。。。这是代码:

Page.aspx。cs

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btn.Click += new EventHandler(btn_Click);
        }
        void btn_Click(object sender, EventArgs e)
        {
            Student student = new Student() { Id = 1, Name = "John" };
            int rowsAffected = Student.AddStudent(student);
            Response.Write(rowsAffected);
        }
    }

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public static int AddStudent(Student s)
        {
            string conString = System.Configuration.ConfigurationManager.ConnectionStrings["string1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Students (Name) VALUES (@Name)", con);
                cmd.Parameters.AddWithValue("@Name", s.Name);
                return cmd.ExecuteNonQuery();
            }
        }
    }

请尝试修改它以满足您的需求,如果它最终有效,请告诉我。它有一些问题(比如没有把学生放在一个单独的文件中),但我希望你能明白。