VS2008 更新数据源问题
静之深 2010-07-22 12:48:40 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 更新数据源//又问题 未完成
{
public partial class Form1 : Form
{
SqlConnection conn;
DataSet ds;
SqlDataAdapter sda;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=.;database=practice;uid=sa;pwd=123123123");
SqlCommand cmd = new SqlCommand("select * from student", conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds, "student");
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["student"];
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(txtNo.Text);
//dr["xuehao"] = txtNo.Text.Trim();
dr["xingming"] = txtName.Text.Trim();
dr["dianhua"] = txtTel.Text.Trim();
SqlCommandBuilder cmdbuider = new SqlCommandBuilder(sda);
sda.Update(dt);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
txtTel.Text = dataGridView1.SelectedCells[2].Value.ToString();
}
}
}
这是一个更新数据源的问题:其中student表中有xuehao xingming dianhua,xuehao 为主键
在运行中出现错误 :
未将对象引用设置到对象的实例。
哪位大虾帮帮小弟吧