我究竟做错了什么 ? [英] what am I doing wrong ?
问题描述
我是c#和.net的新手。我写了一个小程序来添加行访问
表。
程序通过动作但数据永远不会到达那里。
这是我的代码。我故意不使用表单控件。我试图通过代码单独完成
。您可以放心地假设我有一个访问datsabase
,其中一个表名称为三列(字段)。
我哪里出错?
>
OleDbConnection con;
OleDbDataAdapter dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection(testacc.Properties.Settings.Defaul t.mytestConnectionString);
dbad = new OleDbDataAdapter(" select * from who,con);
ds = new DataSet();
dbad.Fill(ds," who");
dr = ds.Tables [" who" ;]。NewRow();
dr [" name"] =" Joe";
dr [" adress"] =" 123 anystreet" ;;
dr [" age"] = 25;
ds.Tables [" who"]。Rows.Add(dr);
con.Close();
提前提供所有帮助的
Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using the form controls. I"m trying
to do it solely thru code. You can safely assume I have an access datsabase
with one table called who with three columns(fields).
where did i go wrong ?
OleDbConnection con;
OleDbDataAdapter dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection(testacc.Properties.Settings.Defaul t.mytestConnectionString);
dbad = new OleDbDataAdapter("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
con.Close();
thanx in advance for all your help
推荐答案
只要快速看一下,看起来你的联系就好了打开。
我对oledbconnection对象没有太多帮助,但我认为这是你问题的一部分。它可能不是全部。
Just looking at it quickly, it looks like you''re connection isn''t open.
I haven''t done much with the oledbconnection object but I think that''s
part of your problem. It may not be all of it though.
我添加了con.open()
它没有做任何差不多。
就像我说的那样。
我没有错!它完成了进程。当我检查数据库时
是没有数据。
" Doug"写道:
I added con.open()
it didn''y make any difference.
like I said.
I get no errors ! It completes the proccess. when I check the database there
is no data.
"Doug" wrote:
只是快速看一下,看起来你连接都没有打开。
我没有''使用oledbconnection对象做了很多事情,但我认为这是你问题的一部分。它可能不是全部。
Just looking at it quickly, it looks like you''re connection isn''t open.
I haven''t done much with the oledbconnection object but I think that''s
part of your problem. It may not be all of it though.
Slimshin,
您的代码不是完成更新,你可以添加这个,
Slimshin,
Your code is not complete to update, you can add this,
OleDbConnection con;
OleDbConnection con;
尝试
{
try
{
OleDbDataAdapter dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection(testacc.Properties.Settings.Defaul t.mytestConnectionString);
dbad = new OleDbDataAdapter(" select * from who",con);
ds = new DataSet();
dbad.Fill (ds,who);
dr = ds.Tables [" who"]。NewRow();
dr [" name"] =" ; Joe" ;;
dr [" adress"] =" 123 anystreet";
dr [" age"] = 25;
ds.Tables [" who"]。Rows.Add(dr);
OleDbDataAdapter dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection(testacc.Properties.Settings.Defaul t.mytestConnectionString);
dbad = new OleDbDataAdapter("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
SQLCommandBuilder cmd = new SQLCommandBuilder();
cm = cmd(OleDBbDataAdapter);
OleDbDataAdapter.Fill (ds);
}
catch(exception ex)
{
MessageBox.Show(ex.ToString ());
}
finnaly
{
con.close;
}
现在你可以看到如果仍然发生了什么问题(我在这条消息中直接输入了这个
并且很长时间没有使用命令编译器。
因为它正在工作,你可以通过实例使用使用来重构你的代码。对于
连接确实让它看起来更漂亮。
我希望这会有所帮助,
Cor >
SQLCommandBuilder cmd = new SQLCommandBuilder();
cm = cmd(OleDBbDataAdapter);
OleDbDataAdapter.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finnaly
{
con.close;
}
Now you can see what goes wrong if that still happen (I have typed this
direct in this message and did a long time not use the commandbuilder).
As it is working you can refactor your code, by instance using "using" for
the connection does make it look nicer.
I hope this helps,
Cor
con.Close();
con.Close();
这篇关于我究竟做错了什么 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!