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

C# 无法使用C visual studio 2008将数据插入表中,c#,.net,sql,sql-server,connection,C#,.net,Sql,Sql Server,Connection,您好,每个人,我试图创建简单的学生表单,将数据添加到数据库中,但无法做到这一点,在程序执行过程中没有错误,应用程序运行良好,但表中没有任何更改,n值未插入表中,有什么问题请告诉我,我正在编写下面的代码。。 提前问: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data; using Sys

您好,每个人,我试图创建简单的学生表单,将数据添加到数据库中,但无法做到这一点,在程序执行过程中没有错误,应用程序运行良好,但表中没有任何更改,n值未插入表中,有什么问题请告诉我,我正在编写下面的代码。。 提前问:

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.BeginTransactionSolationLevel.ReadCommitted


一,。不要传递太多参数。如果你想增加一名学生,那应该会让你大吃一惊。只传递一个参数-使用所需值填充的class Student

二,。我认为这里没有必要进行交易。您只想推送一个对象,因此如果它失败,结果是相同的-不做任何更改

三,。正如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();
            }
        }
    }

请尝试修改它,以满足您的需要,并让我知道,如果它最终工作。它有一些问题,比如没有将班级学生放在单独的文件中,但我希望你能理解。

你的一些参数有@和一些没有@是有原因的吗?没有解决方案,兄弟,我已经更正了我的代码,程序运行和执行良好,但值未插入到tableGoto SQL Server Management Studio和数据库中执行:选择DB_ID根据此数据库ID运行探查器,查看是否在服务器级别进行任何活动。我正在使用visual Studio内置的SQL Server尝试此操作,我没有使用management studio…没有解决方案,兄弟,我已经更正了我的代码,程序运行和执行很好,但是值没有插入到表中。尝试了这一个SqlCommand cmd=new SqlCommandINSERT-into-Student[Title]、[FirstName]、[LastName]、[FatherName]、[Gender]、[Class]、[Section]、[Age]、[DateOfBirth]、[religation],[种姓],[图像],[地址],[家庭电话],[手机],[电子邮件] VALUES@Title,@FirstName、@LastName、@FatherName、@Gender、@Class、@Section、@Age、@DateOFBirth、@religation、@Caste、@Image、@Address、@HomePhone、@mobile、@Email、con;这个怎么样?SqlTransaction t=con.beginTransactionsolationlevel.readcommitten对它进行了三次修改,但我找不到解决方案:是的,用更少的参数进行了尝试e查询,但结果相同:,我必须从解决方案资源管理器手动将详细信息输入表中。它没有从@mujahid表单中获取值。您的函数是否返回任何错误或其他内容?您得到了什么输出?您是否向表中提供了所有必填值?函数Addstudent返回整数值,我没有收到任何错误ors程序正在成功运行,但我看不到插入的值table@mujahid,好吧,这太奇怪了…试着一行一行地调试你的应用程序。这应该会给我们一个错误的线索。我打赌有一些非常明显的东西,没有人检查它。。。