dataset.updata()更新数据库 为什么老出错 20
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
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 StudentManagementSystem
{
public partial class stuUpdate : Form
{
private DataSet UpdClassStudSet = new DataSet();
private DataSet UpdNameStudSet = new DataSet();
private string queryStrByClass;
private string queryStrByName;
private SqlConnection conn;
private SqlDataAdapter sda;
public stuUpdate()
{
InitializeComponent();
}
//在数据库中检索班级名称并填充到班级列表
private void stuUpdate_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string queryString = "select Class_name from class";
conn = CommonClass.DBConn.StuMSCon();
//conn.Open();
sda = new SqlDataAdapter(queryString,conn);
sda.Fill(ds,"ds");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
cbByClass.Items.Add(ds.Tables[0].Rows[i][0]);
cbByClass.SelectedIndex = 0; //指定初始选项,此处为第一个记录
//conn.Close();
}
//按班级检索
private void btnSerch2_Click(object sender, EventArgs e)
{
queryStrByClass = "select Student_id as 学号,Student_name as 姓名,Sex as 性别,Year(getdate())-Year(Birth) as 年龄," +
" Class_name as 班级 from student,class where student.Class_id=class.Class_id and class.Class_name='" +
this.cbByClass.SelectedItem.ToString().Trim() + "'";
//conn = CommonClass.DBConn.StuMSCon();
//conn.Open();
sda = new SqlDataAdapter(queryStrByClass, conn);
UpdClassStudSet.Clear();
sda.Fill(UpdClassStudSet, "student2");
dgvResult.DataSource = UpdClassStudSet.Tables["student2"];
//conn.Close();
}
//保存修改
private void btnSave_Click(object sender, EventArgs e)
{
if (UpdClassStudSet.HasChanges()) //如果改动是在按班级检索的结果集上进行的,则更改该结果集
{
sda = new SqlDataAdapter(queryStrByClass,conn);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Update(UpdClassStudSet.GetChanges(), "student2"); //这里提示出错:对于多个基表不支持动态 SQL 生成!!
}
}
}
}
我想实现的功能:窗体加载时把班级信息加载到combox,点击“检索”时可以查询对应班级学生的信息。这里用的dataset,我想直接在窗体里更改信息,然后让它更新到数据库。但执行到sda.Update(UpdClassStudSet.GetChanges(), "student2"); 时出错,提示为:对于多个基表不支持动态 SQL 生成。
请问错误原因在哪?改如何修改呢? 展开
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 StudentManagementSystem
{
public partial class stuUpdate : Form
{
private DataSet UpdClassStudSet = new DataSet();
private DataSet UpdNameStudSet = new DataSet();
private string queryStrByClass;
private string queryStrByName;
private SqlConnection conn;
private SqlDataAdapter sda;
public stuUpdate()
{
InitializeComponent();
}
//在数据库中检索班级名称并填充到班级列表
private void stuUpdate_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string queryString = "select Class_name from class";
conn = CommonClass.DBConn.StuMSCon();
//conn.Open();
sda = new SqlDataAdapter(queryString,conn);
sda.Fill(ds,"ds");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
cbByClass.Items.Add(ds.Tables[0].Rows[i][0]);
cbByClass.SelectedIndex = 0; //指定初始选项,此处为第一个记录
//conn.Close();
}
//按班级检索
private void btnSerch2_Click(object sender, EventArgs e)
{
queryStrByClass = "select Student_id as 学号,Student_name as 姓名,Sex as 性别,Year(getdate())-Year(Birth) as 年龄," +
" Class_name as 班级 from student,class where student.Class_id=class.Class_id and class.Class_name='" +
this.cbByClass.SelectedItem.ToString().Trim() + "'";
//conn = CommonClass.DBConn.StuMSCon();
//conn.Open();
sda = new SqlDataAdapter(queryStrByClass, conn);
UpdClassStudSet.Clear();
sda.Fill(UpdClassStudSet, "student2");
dgvResult.DataSource = UpdClassStudSet.Tables["student2"];
//conn.Close();
}
//保存修改
private void btnSave_Click(object sender, EventArgs e)
{
if (UpdClassStudSet.HasChanges()) //如果改动是在按班级检索的结果集上进行的,则更改该结果集
{
sda = new SqlDataAdapter(queryStrByClass,conn);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Update(UpdClassStudSet.GetChanges(), "student2"); //这里提示出错:对于多个基表不支持动态 SQL 生成!!
}
}
}
}
我想实现的功能:窗体加载时把班级信息加载到combox,点击“检索”时可以查询对应班级学生的信息。这里用的dataset,我想直接在窗体里更改信息,然后让它更新到数据库。但执行到sda.Update(UpdClassStudSet.GetChanges(), "student2"); 时出错,提示为:对于多个基表不支持动态 SQL 生成。
请问错误原因在哪?改如何修改呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询