具体代码如下
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;
using System.Data.SqlClient;
using System.Windows.Forms;namespace student
{
public partial class adslstucs : Form
{
SqlDataAdapter sqlda;
DataSet sqlds;
public adslstucs()
{
InitializeComponent();
} private void adslstucs_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentsysDataSet.student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.studentsysDataSet.student);
SqlConnection conn = new SqlConnection("server=sam-pc;database=studentsys;uid=sa;pwd=sa");
conn.Open();
SqlDataAdapter sqlda;
DataSet sqlds;
sqlda = new SqlDataAdapter("select * from student", conn);
sqlds = new DataSet();
sqlda.Fill(sqlds, "student");
dataGridView1.DataSource = sqlds.Tables["student"];
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sqlda); } private void button1_Click(object sender, EventArgs e)
{
addstu Main = new addstu();
Main.Show();
} private void button2_Click(object sender, EventArgs e)
{
destu Main = new destu();
Main.Show();
} private void button3_Click(object sender, EventArgs e)
{
if (sqlds.HasChanges())
{
try {
sqlda.Update(sqlds.Tables["student"]);
sqlds.Tables["student"].AcceptChanges();
MessageBox.Show("更新成功", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex){
MessageBox.Show(ex.Message,"更新失败",MessageBoxButtons.OK,MessageBoxIcon.Error);} }
}
}
}
我看了论坛上的代码打出来不知道为啥没有反应只能用作刷新,
谢谢各位了
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;
using System.Data.SqlClient;
using System.Windows.Forms;namespace student
{
public partial class adslstucs : Form
{
SqlDataAdapter sqlda;
DataSet sqlds;
public adslstucs()
{
InitializeComponent();
} private void adslstucs_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentsysDataSet.student”中。您可以根据需要移动或删除它。
this.studentTableAdapter.Fill(this.studentsysDataSet.student);
SqlConnection conn = new SqlConnection("server=sam-pc;database=studentsys;uid=sa;pwd=sa");
conn.Open();
SqlDataAdapter sqlda;
DataSet sqlds;
sqlda = new SqlDataAdapter("select * from student", conn);
sqlds = new DataSet();
sqlda.Fill(sqlds, "student");
dataGridView1.DataSource = sqlds.Tables["student"];
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sqlda); } private void button1_Click(object sender, EventArgs e)
{
addstu Main = new addstu();
Main.Show();
} private void button2_Click(object sender, EventArgs e)
{
destu Main = new destu();
Main.Show();
} private void button3_Click(object sender, EventArgs e)
{
if (sqlds.HasChanges())
{
try {
sqlda.Update(sqlds.Tables["student"]);
sqlds.Tables["student"].AcceptChanges();
MessageBox.Show("更新成功", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex){
MessageBox.Show(ex.Message,"更新失败",MessageBoxButtons.OK,MessageBoxIcon.Error);} }
}
}
}
我看了论坛上的代码打出来不知道为啥没有反应只能用作刷新,
谢谢各位了
这条语句移到
sqlda.Update(sqlds.Tables["student"]);
之前
未将对象引用设置到对象实例
未将对象引用设置到对象实例
这是怎么回事
this.studentTableAdapter.Fill(this.studentsysDataSet.student);
conn.Open();
SqlDataAdapter sqlda;
DataSet sqlds;
你引用的对象本来就是null,你看看Load中怎么还有这个: SqlDataAdapter sqlda; DataSet sqlds;
其实在全局变量中已经有了,但是你在 button3_Click(object sender, EventArgs e)这个事件中引用的正式全局变量的sqlds所以没有给他赋值,你还是把Load中的SqlDataAdapter sqlda; DataSet sqlds;这两个去掉就OK了。