保存不进去

mingcsharp 2012-03-26 10:56:54
string sql = "select monthY,ProcessID,WorkReward,OtherReward,AveReward,Persons,CalcDate,Remarks from MonthPreReward";// where monthY='" + monthy + "'";
SqlConnection con = new SqlConnection(Config.ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(sql, con);
dt = new DataTable();
ada.Fill(dt);
dgview.DataSource = dt.DefaultView;

string sql = @"select monthY,ProcessID,WorkReward,OtherReward,AveReward,Persons,CalcDate,Remarks from MonthPreReward";
SqlConnection con = new SqlConnection(Config.ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(sql, con);
SqlCommandBuilder build = new SqlCommandBuilder(ada);
ada.Update(dt);
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
谁帮我看看这点代码,上面是加载,下面是保存,不知道为什么保存不进去,总是出错
提示说datatable为空,加载的时候明明有东西了,dt是个公有的datatable
...全文
148 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
没有,全部代码就那些
macooidle 2012-03-26
  • 打赏
  • 举报
回复
你是不是那里开了个事务?
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
没有错误,就是点保存了数据库里没有
yibey 2012-03-26
  • 打赏
  • 举报
回复
我参照楼主的写法重写了下保存是可以的。。。楼主可以贴出错误信息?
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bkq421511585 的回复:]
怎么 2个SqlDataAdapter 了 我写了个demo 你看看

C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using Sy……
[/Quote]
我是直接在dgv里面改值然后保存的,看上去不是和你这个一样吗,不过你共用了两个变量,我特意在网上看的,只要数据架构一样语句一样就可以保存啊,以前我保存的好好的,今天就不行了,见鬼
csrwgs 2012-03-26
  • 打赏
  • 举报
回复
你有UpdateCommand吗?
冰镇宝贝321 2012-03-26
  • 打赏
  • 举报
回复
怎么 2个SqlDataAdapter 了 我写了个demo 你看看

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;
using System.Data.Common;

namespace qqq
{
public partial class dateset : Form
{
public dateset()
{
InitializeComponent();
}

SqlConnection conn;
SqlDataAdapter sda;
DataSet ds;
int m;
private void dateset_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=.;Initial Catalog=TQMS_QuestionManager;User ID=sa;Password=123");
SqlCommand cmd = new SqlCommand("select * from dbo.TQ_UserInfo", conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds, "aaaa");
dataGridView1.DataSource = ds.Tables[0];
bnt2.Visible = false;
groupBox2.Visible = false;
m = 1;
}

private void btn1_Click(object sender, EventArgs e)
{
conn=new SqlConnection("Data Source=.;Initial Catalog=TQMS_QuestionManager;User ID=sa;Password=123");
SqlCommand cmd = new SqlCommand("select * from dbo.TQ_Chapters",conn);
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
sda.Fill(ds,"bbb");
dataGridView1.DataSource = ds.Tables[0];
groupBox1.Visible = false;
groupBox2.Visible = true;
btn1.Visible = false;
bnt2.Visible = true;
m = 2;
}

private void update_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["aaaa"];
sda.FillSchema(dt, SchemaType.Mapped);
DataRow dr = dt.Rows.Find(TxtID.Text);
dr["TQ_UserName"] = txtName.Text.Trim();
dr["TQ_TureName"]=txtTureName.Text.Trim();
dr["TQ_UserPwd"]=txtPassWord.Text.Trim();
dr["TQ_Email"]=txtEmail.Text.Trim();
dr["TQ_UserType"] = txtUserType.Text.Trim();
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
sda.Update(dt);
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);

}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(m == 1)
{
TxtID.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
txtTureName.Text=dataGridView1.SelectedCells[2].Value.ToString();
txtPassWord.Text = dataGridView1.SelectedCells[3].Value.ToString();
txtEmail.Text = dataGridView1.SelectedCells[4].Value.ToString();
txtUserType.Text = dataGridView1.SelectedCells[5].Value.ToString();
}
else
{
txtChapterID.Text = dataGridView1.SelectedCells[0].Value.ToString();
txtChapterName.Text = dataGridView1.SelectedCells[1].Value.ToString();
txtCourseID.Text = dataGridView1.SelectedCells[2].Value.ToString();
}
}

private void bnt2_Click(object sender, EventArgs e)
{
dateset_Load(sender,e);
btn1.Visible = true;
bnt2.Visible = false;
groupBox1.Visible = true;
groupBox2.Visible = false;
}
}
}



mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
我一直都这样保存好好的,在别的地方就可以,今天就是不行见鬼,
大哥你说的什么意思,能不能能点具体的,加载和保存代码,让学习一下,谢谢
bdmh 2012-03-26
  • 打赏
  • 举报
回复
你下面的保存,为什么又new了一个 SqlDataAdapter ada 啊,如果要保存也应该是针对上面那个保存啊
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
看来只能我慢慢研究了
bearlin081001 2012-03-26
  • 打赏
  • 举报
回复

我的意思是 你再定义一个公有的dt1,加载的时候 让dt1=dt,
然后在保存的时候 ada.Update(dt1);
看看dt1里面是否有内容,
这是你代码里新定义的datatable,除了这两个地方没别的地方用到

ada.Update(dt1)中
如果dt1有值说明你代码中 不知道什么操作把原来的 dt的内容清了,
如果没值就是和dgview.EndEdit()这个方法有关呗
yibey 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wmingcsharp 的回复:]

经过多次测试发现一个问题,求帮看看,
我在dgv里面输入一行之后,把焦点移到别的行上点保存就可以保存进去,要是不移,焦点还在当前行(就是刚才输入的行)点保存就保存不进去,
说明一下,
保存时我调用过dgview.EndEdit();
不明白这是为什么,求高手现身
[/Quote]
你在调试的时候查看下保存update这个操作后面dt监视下看看是否是你想要的数据,
如果不是那就说明是后台数据没及时更新。所以你想哥办法主动更新后台下;如果不是拿我就不清楚了
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
没有你说的这
就一个dt
bearlin081001 2012-03-26
  • 打赏
  • 举报
回复
ada.Update(dt);

这个dt 估计你在别的地方不小心给清了

你再定义个 dt1
加载的时候后面加个 dt1= dt
然后
ada.Update(dt1);
这dt1是新定义的,不可能被清了,试试吧
mingcsharp 2012-03-26
  • 打赏
  • 举报
回复
经过多次测试发现一个问题,求帮看看,
我在dgv里面输入一行之后,把焦点移到别的行上点保存就可以保存进去,要是不移,焦点还在当前行(就是刚才输入的行)点保存就保存不进去,
说明一下,
保存时我调用过dgview.EndEdit();
不明白这是为什么,求高手现身

110,534

社区成员

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

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

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