未处理的SQLException 列名无效

tasaki 2008-09-18 06:34:52
在Winform 的修改DATAGRIDVIEW 更新DS 和数据库中的值的函数中,总是出现了如下问题,百思不得其解

public bool update()
{
SqlParameter para = new SqlParameter();
da.UpdateCommand = myConnection.CreateCommand();

da.UpdateCommand.CommandText = "UPDATE pnametable SET met=@met,explain=@explain,pname=@pname,price=@price WHERE aoto_ID=@autoid";

da.UpdateCommand.Parameters.Add("@met", SqlDbType.VarChar, 50, "met");
da.UpdateCommand.Parameters.Add("@explain", SqlDbType.Text, 16,"explain");
da.UpdateCommand.Parameters.Add("@price",SqlDbType.Int,4,"price");
da.UpdateCommand.Parameters.Add("@pname",SqlDbType.VarChar,50,"pname");

para = da.UpdateCommand.Parameters.Add("@autoid", SqlDbType.Int,4, "aoto_ID");
para.SourceVersion = DataRowVersion.Original;

//MessageBox.Show(da.UpdateCommand.CommandText); 如果该行执行结果为正常的SQL语句,语句如下
int count = da.Update(ds.Tables["scores2"]);
//MessageBox.Show(ds.Tables["scores2"].Rows.Count.ToString()); 如果该行执行结果为行数,DS正常
bool result = count > 0 ? true : false;
return result;
//return true;
}

出错症状为

int count = da.Update(ds.Tables["scores2"]); 该行
未处理的SQLException
列名 'met' 无效。
列名 'aoto_ID' 无效。


注释掉的 MESSAGEBOX得到的SQL语句 UPDATE pnametable SET met=@met,explain=@explain,pname=@pname,price=@price WHERE aoto_ID=@autoid
其中数据库中的KEY为 aoto_ID

在ds.Tables["scores2"] 表中,所需要的数据和字段都正常存在

以下是DS的赋值函数

private SqlConnection myConnection = new SqlConnection("server=192.168.0.223;database=Tasaki_zealo;pwd=sa;uid=sa");
private SqlCommand myComd;
private SqlDataAdapter da = null;
private DataSet ds = new DataSet();

......

public void showPicList_all(string did)
{
dataGridView2.Columns.Clear();

string strSel = "SELECT * FROM picdb";
da = new SqlDataAdapter(strSel, myConnection);
ds = new DataSet();
da.Fill(ds, "scores2");

dataGridView2.DataSource = ds.Tables["scores2"].DefaultView;
}
...全文
696 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tasaki 2008-09-23
  • 打赏
  • 举报
回复
有人能帮忙解决吗,拖了很久的帖子了
diffmaker 2008-09-19
  • 打赏
  • 举报
回复
没有使用你的代码

da.UpdateCommand.CommandText = "UPDATE pnametable SET met=@met,explain=@explain,pname=@pname,price=@price WHERE aoto_ID=@autoid";

da.UpdateCommand.Parameters.Add("@met", SqlDbType.VarChar, 50, "met");
da.UpdateCommand.Parameters.Add("@explain", SqlDbType.Text, 16,"explain");
da.UpdateCommand.Parameters.Add("@price",SqlDbType.Int,4,"price");
da.UpdateCommand.Parameters.Add("@pname",SqlDbType.VarChar,50,"pname");
diffmaker 2008-09-19
  • 打赏
  • 举报
回复
原来是WinForm啊,我测试了一下,代码非常简单,测试成功。

主窗口中只有一个DataGridView和一个Button,取名为dgvTest、btnUpdate,为btnUpdate绑定事件btnUpdate_Click,代码如下:

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 WADataGridView
{
public partial class Form1 : Form
{
private DataSet ds = new DataSet();
private SqlDataAdapter sda;
public Form1()
{
InitializeComponent();
InitData(); }

private void InitData()
{
string constr = "Data Source=***;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***";
string sql = "select sid,Name,Subject,Result from t_test_b";
sda = new SqlDataAdapter(sql, constr);
sda.Fill(ds, "S1");
DataTable dt = ds.Tables["S1"];
this.dgvTest.DataSource = dt;
}

private void btnUpdate_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
int erows = sda.Update(ds, "S1");
MessageBox.Show(erows.ToString());
}
}
}
tasaki 2008-09-19
  • 打赏
  • 举报
回复
你的程序没问题,但是我的程序是面对DATAGRIDVIEW的列操作的,不是这样的,还是谢谢你
diffmaker 2008-09-18
  • 打赏
  • 举报
回复
前后数据变化
--------------------------
1 张三 语文 74
2 张三 数学 83
3 张三 物理 93
4 李四 语文 74
5 李四 数学 84
6 李四 物理 94
7 王五 物理 66
8 王五 英语 99
--------------------------
1 张三 语文 74
2 张三 数学 83
4 李四 语文 74
5 李四 数学 84
6 李四 物理 94
7 王五 物理 66
8 乌贼 英语 99
9 鳄鱼 英语 80
diffmaker 2008-09-18
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string constr = "Data Source=***;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***";
string sql = "select sid,Name,Subject,Result from t_test_b";
SqlDataAdapter sda = new SqlDataAdapter(sql, constr);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();
sda.Fill(ds, "S1");
DataTable dt = ds.Tables["S1"];
for (int i = 0; i < dt.Rows.Count; i++)
{
string sid = dt.Rows[i][0].ToString();
string name = dt.Rows[i][1].ToString();
string sub = dt.Rows[i][2].ToString();
string rst = dt.Rows[i][3].ToString();
Console.WriteLine("{0}\t{1}\t{2}\t{3}", sid, name, sub, rst);
}
//添加记录
DataRow dr = dt.NewRow();
dr[1] = "鳄鱼";
dr[2] = "英语";
dr[3] = 80;
dt.Rows.Add(dr);

//修改记录
dt.Rows[dt.Rows.Count - 2][1] = "乌贼";

//删除记录
dt.Rows[2].Delete();

int erows = sda.Update(ds, "S1");
Console.WriteLine("{0} rows updated", erows);

Console.ReadKey();
}
diffmaker 2008-09-18
  • 打赏
  • 举报
回复
可能我们的思路不同,我也是在学习阶段,记得Update()使用没有你那么复杂,刚才测试了一下下面的代码,运行是正常的,不知道对你是不是有帮助。
tasaki 2008-09-18
  • 打赏
  • 举报
回复
LS的兄弟,你哪段看不懂,我解释给你听
diffmaker 2008-09-18
  • 打赏
  • 举报
回复
代码真复杂,不能简化吗?
tasaki 2008-09-18
  • 打赏
  • 举报
回复
我这乞丐第一次发帖都拿出50分来,却没人回,唉~~~
tasaki 2008-09-18
  • 打赏
  • 举报
回复
朋友,你等级这么高,给点意见吧,这个问题困了我好几天了,眼看这我一天比一天消瘦
yuchong1984 2008-09-18
  • 打赏
  • 举报
回复
向你学习!
tasaki 2008-09-18
  • 打赏
  • 举报
回复
public void showPicList_all(string did)
{
dataGridView2.Columns.Clear();

string strSel = "SELECT * FROM picdb";
da = new SqlDataAdapter(strSel, myConnection);
ds = new DataSet();
da.Fill(ds, "scores2");

dataGridView2.DataSource = ds.Tables["scores2"].DefaultView;
}

就是这个呀,我发帖子的最后一行
lovehongyun 2008-09-18
  • 打赏
  • 举报
回复
发你填充ds的sql语句看看.
tasaki 2008-09-18
  • 打赏
  • 举报
回复
在线顶

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧