SqlDataAdapter sda = null;
DataSet ds = null;
SqlCommandBuilder scb = new SqlCommandBuilder();
private void button1_Click(object sender, EventArgs e)
{
dbhlpe.conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from FamilyDB where famName ='" + this.text.Trim() + "' and exists(select * from FamilyDB where famdate between '" + Convert.ToDateTime(this.dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(this.dateTimePicker2.Text) + "')", dbhlpe.conn);
DataSet ds = new DataSet();
scb = new SqlCommandBuilder(sda);
sda.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dbhlpe.conn.Close();
} private void update_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = this.text.Trim();
} private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
更新的时候提示 未将对象引用设置到对象的实例。
private void button3_Click(object sender, EventArgs e)
{
int index = dataGridView1.SelectedRows[0].Index;
ds.Tables[0].Rows[index].Delete();
}
还有这个删除,我是按照老师的方法写的,删除的时候也出错,麻烦大大看看。
DataSet ds = null;
SqlCommandBuilder scb = new SqlCommandBuilder();
private void button1_Click(object sender, EventArgs e)
{
dbhlpe.conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from FamilyDB where famName ='" + this.text.Trim() + "' and exists(select * from FamilyDB where famdate between '" + Convert.ToDateTime(this.dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(this.dateTimePicker2.Text) + "')", dbhlpe.conn);
DataSet ds = new DataSet();
scb = new SqlCommandBuilder(sda);
sda.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dbhlpe.conn.Close();
} private void update_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = this.text.Trim();
} private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
更新的时候提示 未将对象引用设置到对象的实例。
private void button3_Click(object sender, EventArgs e)
{
int index = dataGridView1.SelectedRows[0].Index;
ds.Tables[0].Rows[index].Delete();
}
还有这个删除,我是按照老师的方法写的,删除的时候也出错,麻烦大大看看。
{
int index = dataGridView1.SelectedRows[0].Index;
ds.Tables[0].Rows[e.index].Delete();
} 这个只是内存中的删除,数据那边是没有删除的。
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
改成这个样子。private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(dataGridView1);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
比如你定义的DataSet ds = new DataSet();
仅在这两个大括号之间有用。
sda.Update方法 有问题不能更新数据库
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(dataGridView1);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
} 改成这样语法报错了
错误 1 与“System.Data.Common.DbDataAdapter.Update(System.Data.DataRow[])”最匹配的重载方法具有一些无效参数 E:\资料\未完成的\Familyfunciton\Familyfunciton\update.cs 43 24 Familyfunciton
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Familyfunciton
{
public partial class update : Form
{
string text;
public update(string s)
{
this.text = s;
InitializeComponent();
}
SqlDataAdapter sda = null;
DataSet ds = null;
SqlCommandBuilder scb = new SqlCommandBuilder();
private void button1_Click(object sender, EventArgs e)
{
dbhlpe.conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from FamilyDB where famName ='" + this.text.Trim() + "' and exists(select * from FamilyDB where famdate between '" + Convert.ToDateTime(this.dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(this.dateTimePicker2.Text) + "')", dbhlpe.conn);
DataSet ds = new DataSet();
scb = new SqlCommandBuilder(sda);
sda.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dbhlpe.conn.Close();
} private void update_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = this.text.Trim();
} private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
} private void button3_Click(object sender, EventArgs e)
{
int index = dataGridView1.SelectedRows[0].Index;
ds.Tables[0].Rows[index].Delete();
}
}
}
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
这句里的
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
去掉试试,你同一个sda重复SqlCommandBuilder了两次
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
} 告诉你了,红色的地方没有定义。
你可以在前面加个this
SqlDataAdapter sda;
DataSet da;
SqlCommandBuilder scb;
private void button1_Click(object sender, EventArgs e)
{
ds = new DataSet();
scb = new SqlCommandBuilder();
scb.DataSource="数据服器";
scb.InitialCatalog="数据库名";
scb.IntegratedSecurity=true;
dbhlpe.conn.SqlConnection(scb.ConnectionString);
dbhlpe.conn.Open();
sda = new SqlDataAdapter("...sql语句", dbhlpe.conn);
sda.fill(ds);
dataGridView1.DataSource=ds.Tables["表名"];
dbhlpe.conn.Close();
} private void button2_Click(object sender, EventArgs e)
{
sda.UpdateCommand=scb.UpdateCommand;
int count =sda.Update(ds);
if (count > 0)
{
MessageBox.Show("修改数据成功。");
}
} private void button3_Click(object sender, EventArgs e)
{
sda.DeleteCommand=scb.DeleteCommand;
int index = dataGridView1.SelectedRows[0].Index;
ds.Tables[0].Rows[indext].Delete();
sda.Update(ds);
}
这样你试试看.
没细看,可能有问题.
方法有很多种,不能局限于老师所讲的.搞程序就怕在一棵树上吊死.
正常这样更新是不需要上边这两句的,你去掉试试,可能就是这两句的关系