表结构:-- Table: demo-- DROP TABLE demo;CREATE TABLE demo
(
"ID" smallint NOT NULL,
"Name" character(12),
"NO" integer,
CONSTRAINT pk_table_1 PRIMARY KEY ("ID")
)
WITH (OIDS=FALSE);
ALTER TABLE demo OWNER TO demo;Form里放有一个dgv和一个button相关代码如下: NpgsqlConnection conn = new NpgsqlConnection("Server=.;Port=5432;User Id=demo;Password=demo;Database=demo;");
NpgsqlCommand comm = new NpgsqlCommand("select * from demo");
DataSet ds = new DataSet();
NpgsqlDataAdapter da = new NpgsqlDataAdapter();
NpgsqlCommandBuilder cb; private void Form1_Load(object sender, EventArgs e)
{
comm.Connection = conn; da.SelectCommand = comm; cb = new NpgsqlCommandBuilder(da); da.Fill(ds, "demo"); dataGridView1.DataSource = ds.Tables["demo"];
} private void button1_Click(object sender, EventArgs e)
{
if (ds.HasChanges())
{
MessageBox.Show("Yes");
MessageBox.Show(ds.Tables["demo"].Rows[0].RowState.ToString());
da.Update(ds, "demo");
}
else
{
MessageBox.Show("NO");
}
}
当我在dgv里修改一些数据后,ds.HsaChanges()为true,rowstate为modified,但da.Update()之后,数据库里的数据并没有改变,我怎么看也没有找出问题所在。
(
"ID" smallint NOT NULL,
"Name" character(12),
"NO" integer,
CONSTRAINT pk_table_1 PRIMARY KEY ("ID")
)
WITH (OIDS=FALSE);
ALTER TABLE demo OWNER TO demo;Form里放有一个dgv和一个button相关代码如下: NpgsqlConnection conn = new NpgsqlConnection("Server=.;Port=5432;User Id=demo;Password=demo;Database=demo;");
NpgsqlCommand comm = new NpgsqlCommand("select * from demo");
DataSet ds = new DataSet();
NpgsqlDataAdapter da = new NpgsqlDataAdapter();
NpgsqlCommandBuilder cb; private void Form1_Load(object sender, EventArgs e)
{
comm.Connection = conn; da.SelectCommand = comm; cb = new NpgsqlCommandBuilder(da); da.Fill(ds, "demo"); dataGridView1.DataSource = ds.Tables["demo"];
} private void button1_Click(object sender, EventArgs e)
{
if (ds.HasChanges())
{
MessageBox.Show("Yes");
MessageBox.Show(ds.Tables["demo"].Rows[0].RowState.ToString());
da.Update(ds, "demo");
}
else
{
MessageBox.Show("NO");
}
}
当我在dgv里修改一些数据后,ds.HsaChanges()为true,rowstate为modified,但da.Update()之后,数据库里的数据并没有改变,我怎么看也没有找出问题所在。
把可能有异常的地方用try catch ,然后看是不是捕获到了异常。
你后台用代码改变datatable中的数据,然后update,看看能不能改变。
http://topic.csdn.net/u/20081208/11/4C712B0A-CB2B-4B42-8B22-102F2B0D48FD.html
26楼的回答
1楼说看异常的时候我就加了如下代码: try
{ da.Update(ds, "demo");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
确实没有异常。
fill()的时候会自动打开连接,完成之后又会自己关闭。
http://www.cnblogs.com/ekinyang/archive/2005/12/30/308338.html
我在发帖之前已经看了这个帖子了,现在的问题不是rowstate没有改变,通过
MessageBox.Show(ds.Tables["demo"].Rows[0].RowState.ToString());可以看到修改后的rowstate已经是modified了。仍然不行。
我使用如下代码跟踪ds的值,发现ds的值已是修改后的值: foreach (DataRow row in ds.Tables["demo"].Rows)
{
MessageBox.Show(row["NO"].ToString());
}
%t 日志: 无法从客户端获得数据: No connection could be made because the target machine actively refused it.
从postgresql读取数据的时候日志不会出现这个错误,而当da.update()的时候日志才会出现这个错误。
我用你的代码试了下,可以。
dataAdapter.UpdateCommand.Parameters.Add("PurchaseNum", OleDbType.Integer, 4, "PurchaseNum");
dataAdapter.UpdateCommand.Parameters.Add("SMemberID", OleDbType.Integer, 4, "SMemberID");
dataAdapter.UpdateCommand.Parameters.Add("CommodityID", OleDbType.Integer, 4, "CommodityID");
不过也不知道你的是什么Connection? NpgsqlConnection? conn ?是否是SqlConnection?是否少了一下东西。
我现在怀疑是不是postgresql的原因了,你看一下我在16楼的回复。
ID NAME NO
=============
5 ked 9
6 wtu 3当我在dgv里修改第一行的NO从9变为8后,再使用update()更新时产生的语句如下:UPDATE "demo"."public"."demo" SET "NO" = 8::int4 WHERE (("ID" = 5::int2) AND ((0::int4 =
1 AND "Name" IS NULL) OR ("Name" = 'ked '::text)) AND ((0::int4 = 1 AND "NO" IS NULL) OR ("NO" = 9::int4)))
ddataRow的rowState来产生commandtext的。