请大家帮我看看,我那里写错了,(新手!!求救啊!)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;namespace app1
{
public partial class Form1 : Form
{
private SqlDataAdapter adap;
private DataSet ds=null;
public Form1()
{
InitializeComponent();
} public void Form1_Load(object sender, EventArgs e)
{
//数据链接字符串
string sql = "data source =.; initial catalog=myschool; user id =sa; password=sa";
//创建connection对象
SqlConnection conn = new SqlConnection(sql);
try
{
//数据库打开
conn.Open();
//创建stringbuilder
StringBuilder sb = new StringBuilder();
//直接搜索表
sb.AppendFormat("select * from student");
//创建dataset对象
this.ds = new DataSet();
//创建sqldataadapter对象
SqlDataAdapter adap = new SqlDataAdapter(sb.ToString(),conn);
//填冲
adap.Fill(ds,"student");
dataGridView1.DataSource = ds.Tables["student"];
//以上都正常,直接在datagridview修改后,点保存按键,就报错! }
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
//以下是单击保存按键事件
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adap);//此处应该是已创建的dataadapter对象!那我怎么调用上边的,我是不是要新建一个!!还是设置一个PRIVATE的公用变量
adap.Update(ds, "student");//报错:未将对象引用设置到对象的实例。
}
}
}
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;namespace app1
{
public partial class Form1 : Form
{
private SqlDataAdapter adap;
private DataSet ds=null;
public Form1()
{
InitializeComponent();
} public void Form1_Load(object sender, EventArgs e)
{
//数据链接字符串
string sql = "data source =.; initial catalog=myschool; user id =sa; password=sa";
//创建connection对象
SqlConnection conn = new SqlConnection(sql);
try
{
//数据库打开
conn.Open();
//创建stringbuilder
StringBuilder sb = new StringBuilder();
//直接搜索表
sb.AppendFormat("select * from student");
//创建dataset对象
this.ds = new DataSet();
//创建sqldataadapter对象
SqlDataAdapter adap = new SqlDataAdapter(sb.ToString(),conn);
//填冲
adap.Fill(ds,"student");
dataGridView1.DataSource = ds.Tables["student"];
//以上都正常,直接在datagridview修改后,点保存按键,就报错! }
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
//以下是单击保存按键事件
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder builder = new SqlCommandBuilder(adap);//此处应该是已创建的dataadapter对象!那我怎么调用上边的,我是不是要新建一个!!还是设置一个PRIVATE的公用变量
adap.Update(ds, "student");//报错:未将对象引用设置到对象的实例。
}
}
}
放到全局中,不要放到Load方法局部