目前我在更新 SQL Server 数据库中的值时遇到问题。
我正在做一个项目,该项目将学生姓名、出生日期和性别存储在数据库中,并且能够通过 Windows 窗体进行编辑和更新。到目前为止浏览数据工作正常,但是当表单上的值发生更改并单击更新按钮时,程序会崩溃并显示
NullReferenceException 未处理。你调用的对象是空的。
错误,我目前不知道是什么原因造成的。
目前我有一个DBConnector
类,它打开与数据库和数据集的连接并将其传递给包含文本框的 Windows 窗体以显示值和导航按钮以浏览数据。(下面是类和windows窗体的代码,我希望能比我解释得更好)。
DBConnector.cs
班级_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace StudentRecords
{
public class DBConnector
{
//Properties
//connection object
private System.Data.SqlClient.SqlConnection Conn;
//dataset object
private StudentsDataSet Ds1;
//dataadapter object
private System.Data.SqlClient.SqlDataAdapter Da;
//constructors
public DBConnector()
{
//call initialisation
init();
}
//Methods
//initialisation method
public void init()
{
//create the memory space for the connection object
Conn = new System.Data.SqlClient.SqlConnection();
//create the memory space for the dataset
Ds1 = new StudentsDataSet();
//set the connection string to the location of our database file
Conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Students.mdf;Integrated Security=True;User Instance=True";
//open the conenction
Conn.Open();
//create a query to get all the records from the table
string query = "SELECT * from Studentstbl";
//create the data adapter for the database
Da = new System.Data.SqlClient.SqlDataAdapter(query, Conn);
//use it to fill the dataset as the first parameter the second is a name for the table we use later on
Da.Fill(Ds1, "Students");
//close the connection
Conn.Close();
System.Windows.Forms.MessageBox.Show("Database connection Open", "Success");
}
public StudentsDataSet DBDataSet
{
get
{
return Ds1;
}
}
//update method
public void UpdateDB(DataSet ds, string table)
{
//create a command builder to reconnect to the database
System.Data.SqlClient.SqlCommandBuilder cb;
//set the comamnd builder to be our existing dataadapter
cb = new System.Data.SqlClient.SqlCommandBuilder(Da);
Da.Update(ds, table);
}
}
}
Windowsstudentdetails.cs
窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StudentRecords
{
public partial class studentdetails : Form
{
DataSet StudentDataSet;
//set variables for limit
int Limit = 0;
int current = 0;
public studentdetails()
{
InitializeComponent();
}
public studentdetails(DataSet ds)
: this()
{
StudentDataSet = ds;
//set the limit of records we can navigate
Limit = StudentDataSet.Tables["Students"].Rows.Count;
NavigateRecords();
}
public studentdetails(DBConnector db) : this()
{ }
public studentdetails(DBConnector db, int sRow) : this(db)
{
current = sRow;
NavigateRecords();
//turn on editing
plEdit.Visible = true;
//set our local dataset object to point to the passed in one
DBConnection = db;
StudentDataSet = db.DBDataSet;
Limit = StudentDataSet.Tables["Students"].Rows.Count;
NavigateRecords();
}
//navigate records function to move through the records
public void NavigateRecords()
{ //create a datarow object and set it to be the first row in the dataset
DataRow dRow = StudentDataSet.Tables["Students"].Rows[current];
//set the form text to add the current record number
this.Text += " for record " + dRow.ItemArray.GetValue(0).ToString();
//fill the text boxes with the database values
txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
txtMiddleName.Text = dRow.ItemArray.GetValue(2).ToString();
txtLastName.Text = dRow.ItemArray.GetValue(3).ToString();
txtDOB.Text = dRow.ItemArray.GetValue(4).ToString();
txtgender.Text = dRow.ItemArray.GetValue(5).ToString();
}
//update the label for the dtatbase
private void UpdateCount()
{
txtCount.Text = (current + 1).ToString() + " of " + Limit.ToString();
}
private void btn_next_Click(object sender, EventArgs e)
{
if (current != Limit - 1)
{
current++;
NavigateRecords();
}
else
{
MessageBox.Show("Last Record", "Information", 0, MessageBoxIcon.Information);
}
}
private void btn_prev_Click(object sender, EventArgs e)
{
if (current > 0)
{
current--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record", "Information", 0, MessageBoxIcon.Information);
}
}
private void btn_Last_Click(object sender, EventArgs e)
{
if (current != Limit - 1)
{
current = Limit - 1;
NavigateRecords();
}
}
private void btn_first_Click(object sender, EventArgs e)
{
if (current != 0)
{
current = 0;
NavigateRecords();
}
}
public DBConnector DBConnection { get; set; }
private void btn_save_Click(object sender, EventArgs e)
{
{
//create a new datarow
DataRow dRow = StudentDataSet.Tables["Students"].NewRow();
//set the data to be the values from the text boxes
dRow[1] = txtFirstName.Text;
dRow[2] = txtMiddleName.Text;
dRow[3] = txtLastName.Text;
dRow[4] = txtDOB.Text;
dRow[5] = txtgender.Text;
//add the row to our dataset
StudentDataSet.Tables["Studentstbl"].Rows.Add(dRow);
//increase the limit as we have a new record
Limit++;
//move to the newly entered Student
current = Limit - 1;
DBConnection.UpdateDB(StudentDataSet, "Students");
MessageBox.Show("Record Saved", "Information", 0, MessageBoxIcon.Information);
NavigateRecords();
}
}
private void btn_update_Click(object sender, EventArgs e)
{
{ //get the current row
DataRow dRow = StudentDataSet.Tables["Students"].Rows[current];
//set the dataset values to those in the textboxes
dRow[1] = txtFirstName.Text;
dRow[2] = txtMiddleName.Text;
dRow[3] = txtLastName.Text;
dRow[4] = txtDOB.Text;
dRow[5] = txtgender.Text;
//call the update method in our DB connector class
DBConnection.UpdateDB(StudentDataSet, "Students");
//display a message box
MessageBox.Show("Record updated", "Information", 0, MessageBoxIcon.Information);
NavigateRecords();
}
}
}
}
调用数据库Students.mdf
和调用表Studentstbl
,在线抛出错误:
DBConnection.UpdateDB(StudentDataSet, "Students");
我知道这可能是我错过的一些愚蠢的事情,但是我对 C# 编码还很陌生,所以如果是这种情况,请原谅我 :)
编辑:我还应该指出,这studentdetails
是一个辅助表单,主表单只包含一个在其中打开该表单的按钮,它简单地包含代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Coursework3
{
public partial class Form1 : Form
{
DBConnector Students;
public Form1()
{
InitializeComponent();
//intansiate the object in memory
Students = new DBConnector();
}
private void btnstudentdetails_Click(object sender, EventArgs e)
{
new studentdetails(Students.DBDataSet).ShowDialog();
}
}
}