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

时间:2012-03-27 14:46:51

标签: c# .net sql sql-server connection

你好每一个我都试图创建简单的学生形式n将数据添加到数据库但是coudlnt这样做,在程序执行期间没有错误,应用程序执行得很好但是表中没有变化,值是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);
        }
    }

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

您的某些参数包含“@”符号,而某些参数则不包含....

答案 2 :(得分:1)

1。)不要传递这么多参数。你想要添加一个学生,这应该响起你所有的钟声。只传递一个参数 - 使用您想要的值填充的类。

2。)我认为这里不需要交易。您只想推送一个对象,因此如果失败,结果是相同的 - 不做任何更改。

3。)正如达人所说,你的查询中没有正确编写的参数

编辑: 刚刚尝试了简化版本,它就像一个魅力...这是代码:

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

请尝试修改它以满足您的需求并让我知道,如果它最终有效。它有一些问题(比如不将学生类放在一个单独的文件中),但我希望你能得到这个想法。