从文本字段更新oledb数据库 [英] update oledb database from text field

查看:60
本文介绍了从文本字段更新oledb数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击保存并且文本字段

已更改时,我需要代码来更新数据库。这应该很容易,因为我使用Microsoft的向导来获得OleDBAdapter和OleDBConnection以及DataSet的
;我所做的就是

在文本字段中显示一条记录,允许用户修改文本

字段,然后在用户点击时再次更新数据库保存

按钮。这些字段已经显示了正确的数据记录,因为我已经为文本字段填写了

DataBinding文本属性。我需要在保存按钮点击事件中输入代码




以下是我已经拥有的部分内容,其中大部分来自向导。 ..

....

使用System.Data;

使用System.Data.OleDb;

....

命名空间EsperanzaThreads

公共类frmComapnyEdit:System.Windows.Forms.Form

....

私人System.Windows.Forms.TextBox txtCompany;

私人System.Windows.Forms.TextBox txtCompanyID;

私人System.Data.OleDb.OleDBCommand oleDbUpdateCommand1;

private EsperanzaThreads.dsCompanyEdit dsCompanyEdit1;

private string strCommandText;

private string strCurrent;

....

public frmComanyEdit(string idname)

{strCurrent = idname;

InitializeComponent();

....

private void InitializeComponent()

{

this.txtCompany = new System.Windows.Forms.TextBox();

this.txtCompanyID = new System.Windows.Forms.TextBox();

this.lblActive = new System.Windows.Forms.Label();

this.lblCompany = new System.Windows.Forms.Label();

this.lblCompanyID = new System.Windows.Forms.Label();

this.oleDbDataAdapterCompanyEdit = new System.Data .OleDb.OleDbDataAdapter();

this.oleDbUpdateCommand1 =新System.Data.OleDb.OleDbCommand();

this.oleDbConnection1 =新System.Data.OleDb.OleDbConnection ();

this.dsCompanyEdit1 = new EsperanzaThreads.dsCompanyEdit();

....

this.txtCompanyID.DataBindings.Add( new System.Windows.Forms.Binding(" Text",

this.dsCompanyEdit1," tblCompany.CompanyId"));

this.oleDbDataAdapterCompanyEdit.TableMappings.Add范围(新

System.Data.Common.DataTableMapping [] {

新的System.Data.Common.DataTableMapping(" Table"," tblCompany",new

System.Data.Common.DataColumn映射[] {

new System.Data.Common.DataColumnMapping(" CompanyId"," CompanyId"),

new System.Data.Common.DataColumnMapping(" ;公司","公司")})});

this.oleDbDataAdapterCompanyEdit.UpdateCommand = this.oleDbUpdateCommand1;


this.oleDbUpdateCommand1.CommandText = UPDATE tblCompany SET CompanyId =?,

Company = ?,Active =? WHERE(CompanyId =?" +")AND(Active =?)AND

(公司=?)" ;;

this.oleDbUpdateCommand1.Connection = this。 oleDbConnection1;

this.oleDbUpdateCommand1.Parameters.Add(new

System.Data.OleDb.OleDbParameter(" CompanyId",

System。 Data.OleDb.OleDbType.VarWChar,1," CompanyId"));

this.oleDbUpdateCommand1.Parameters.Add(新

System.Data.OleDb.OleDbParameter( 公司,

System.Data.OleDb.OleDbType.VarWChar,50,公司));

this.oleDbUpdateCommand1.Parameters.Add(new

System.Data.OleDb.OleDbParameter(Active,

System.Data.OleDb.OleDbType.Boolean,2,Active));

this.oleDbUpdateCommand1.Parameters.Add(new

System.Data.OleDb.OleDbParameter(" Original_Company Id",

System.Data.OleDb。 OleDbType.VarWChar,1,

System.Data.ParameterDirection.Input,false,((System.By te)(0)),

((System.Byte)(0)),CompanyId,System.Data.DataRowVersion.Original,null));

this.oleDbUpdateCommand1.Parameters.Add(new

System.Data.OleDb.OleDbParameter(" Original_Active" ,

System.Data.OleDb.OleDbType.Boolean,2,System.Data.ParameterDirection.Input,

false,((System.Byte)(0)), ((System.Byte)(0)),Active,

System.Data.DataRowVersion.Original,null));

this.oleDbUpdateCommand1.Parameters。添加(新

System.Data.OleDb.OleDbParameter(" Original_Company",

System.Data.OleDb.OleDbType.VarWChar,50,

System.Data.ParameterDirection.Input,false,((System.Byte)(0)),

((System.Byte)(0)),Company,System。 Data.DataRowVersion.Original,null));

....

this.dsCompanyEdit1.DataSetName =" dsCompanyEdit";

。 ...

private void frmCompanyEdit_Load(object sender,System.EventArgs e)

{

strCommandText =" SELECT CompanyID,Company,Active来自tblCompany WHERE

CompanyID =''" + strCurrent + QUOT; '' 英寸;

this.oleDbSelectCommand1.CommandText = strCommandText;

oleDbDataAdapterCompanyEdit.Fill(dsCompanyEdit1);

}


private void btnSave_Click(object sender,System.EventArgs e){

//这里应该去哪里?


以下是我在btnSave_Click中添加新的

记录时的情况,但我认为可能更容易使用更多的

微软的预先编写的代码,我不知道该怎么做。但是,我真正关心的是,当记录被修改为b $ b时,如何更新数据库。我玩这个代码并且无法弄清楚如何将其转换为

做更新。


//代码我用于添加新记录:

DataTable dt = dsCompanyAdd1.Tables [" tblCompany"];

DataRow dr = dt.NewRow();

drNew [" ; CompanyID"] = this.txtCompanyID.Text;

drNew [" Company"] = this.txtCompany.Text;

drNew [" Active"] = this .cmbActive.Text;

dt.Rows.Add(drNew);

oleDbDataAdapterCompanyAdd.Update(dsCompanyAdd1," t blCompany");

dsCompanyAdd1.Clear();

oleDbDataAdapterCompanyAdd.Fill(dsCompanyAdd1);

this.Close();


我发现knowlegebase和帮助文章,但我很困惑,因为

参数,UpdateCommand,数据适配器和数据连接已经由微软的向导编写,我不能''找到任何展示给我的文章

如何使用预先编写的代码。


下面的代码是我能找到的关于如何做我想要的最好的例子,但我仍然无法弄清楚我需要哪些部分,因为微软

预先编写的向导代码。你能告诉我正确的部分和

预先编写的向导代码,或者更好的是,代码应该是什么?
for Save_Click事件?


来自知识库的示例:

custDA.UpdateCommand.Parameters.Add(" @ CustomerID",OleDbType.Char,5,

" CustomerID");


custDA.UpdateCommand.Parameters.Add(" @ CompanyName",OleDbType.VarChar,40,

" CompanyName");


OleDbParameter myParm =

custDA.UpdateCommand.Parameters.Add(" @OldCustomerI D",OleDbType.Char,5,

" CustomerID");

myParm.SourceVersion = DataRowVersion.Original;


public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection conn)

{

OleDbDataAdapter da = new OleDbDataAdapter();

OleDbCommand cmd;

OleDbParameter parm;


//创建UpdateCommand。

cmd = new OleDbCommand(" UPDATE Customers SET CustomerID = @CustomerID,

CompanyName = @CompanyName" +

" WHERE CustomerID = @ oldCustomerID",conn);


cmd.Parameters.Add(" @ CustomerID",OleDbType.Char,5 ,CustomerID);

cmd.Parameters.Add(" @ CompanyName",OleDbType.VarChar,40," CompanyName");


parm = cmd.Parameters.Add(" @ oldCustomerID",OleDbType.Char,5,

" CustomerID");

parm.SourceVersion = DataRowVersion.Original ;


da.UpdateCommand = cmd;


返回da;

}

谢谢,

Pam

I need the code to update the database when Save is clicked and a text field
has changed. This should be very easy since I used Microsoft''s wizards for
the OleDBAdapter and OleDBConnection, and DataSet; and all I''m doing is
showing one record in text fields, allowing the user to modify the text
fields, and then updating the database again when the user clicks the Save
button. The fields already show the correct data record since I have the
DataBinding text property filled in for the text fields. I need the code to
put in the save button click event.

Here are SOME of the portions I already have, most of it from the wizards...
....
using System.Data;
using System.Data.OleDb;
....
namespace EsperanzaThreads
public class frmComapnyEdit : System.Windows.Forms.Form
....
private System.Windows.Forms.TextBox txtCompany;
private System.Windows.Forms.TextBox txtCompanyID;
private System.Data.OleDb.OleDBCommand oleDbUpdateCommand1;
private EsperanzaThreads.dsCompanyEdit dsCompanyEdit1;
private string strCommandText;
private string strCurrent;
....
public frmComanyEdit(string idname)
{strCurrent = idname;
InitializeComponent();
....
private void InitializeComponent()
{
this.txtCompany = new System.Windows.Forms.TextBox();
this.txtCompanyID = new System.Windows.Forms.TextBox();
this.lblActive = new System.Windows.Forms.Label();
this.lblCompany = new System.Windows.Forms.Label();
this.lblCompanyID = new System.Windows.Forms.Label();
this.oleDbDataAdapterCompanyEdit = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.dsCompanyEdit1 = new EsperanzaThreads.dsCompanyEdit();
....
this.txtCompanyID.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.dsCompanyEdit1, "tblCompany.CompanyId"));
this.oleDbDataAdapterCompanyEdit.TableMappings.Add Range(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "tblCompany", new
System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("CompanyId", "CompanyId"),
new System.Data.Common.DataColumnMapping("Company", "Company")})});
this.oleDbDataAdapterCompanyEdit.UpdateCommand = this.oleDbUpdateCommand1;

this.oleDbUpdateCommand1.CommandText = "UPDATE tblCompany SET CompanyId = ?,
Company = ?, Active = ? WHERE (CompanyId = ?" +") AND (Active = ?) AND
(Company = ?)";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("CompanyId",
System.Data.OleDb.OleDbType.VarWChar, 1, "CompanyId"));
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Company",
System.Data.OleDb.OleDbType.VarWChar, 50, "Company"));
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Active",
System.Data.OleDb.OleDbType.Boolean, 2, "Active"));
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_Company Id",
System.Data.OleDb.OleDbType.VarWChar, 1,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "CompanyId", System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_Active" ,
System.Data.OleDb.OleDbType.Boolean, 2, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Active",
System.Data.DataRowVersion.Original, null));
this.oleDbUpdateCommand1.Parameters.Add(new
System.Data.OleDb.OleDbParameter("Original_Company ",
System.Data.OleDb.OleDbType.VarWChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "Company", System.Data.DataRowVersion.Original, null));
....
this.dsCompanyEdit1.DataSetName = "dsCompanyEdit";
....
private void frmCompanyEdit_Load(object sender, System.EventArgs e)
{
strCommandText = "SELECT CompanyID, Company, Active FROM tblCompany WHERE
CompanyID = ''" + strCurrent + "''";
this.oleDbSelectCommand1.CommandText = strCommandText;
oleDbDataAdapterCompanyEdit.Fill(dsCompanyEdit1);
}

private void btnSave_Click(object sender, System.EventArgs e) {
// WHAT SHOULD GO IN HERE?

The following is what I had in the btnSave_Click when I wanted to add a new
record, but I think there is probably an even easier way using more of
Microsoft''s prewritten code and I don''t know how to do it. But what I really
care about is figuring out how to update the database when a record is
modified. I played with this code and can''t figure out how to convert it to
do updates.

//Code I used for adding a new record:
DataTable dt = dsCompanyAdd1.Tables["tblCompany"];
DataRow dr = dt.NewRow();
drNew["CompanyID"] = this.txtCompanyID.Text;
drNew["Company"] = this.txtCompany.Text;
drNew["Active"] = this.cmbActive.Text;
dt.Rows.Add(drNew);
oleDbDataAdapterCompanyAdd.Update(dsCompanyAdd1,"t blCompany");
dsCompanyAdd1.Clear();
oleDbDataAdapterCompanyAdd.Fill(dsCompanyAdd1);
this.Close();

I found knowlegebase and help articles but I am confused since the
parameters, UpdateCommand, data adapter, and data connection are already
written by Microsoft''s wizard and I couldn''t find any articles that showed me
how to use the prewritten code.

The following code is the best example I could find on how to do what I
want, but I still can''t figure out which portions I need, given the Microsoft
prewritten Wizard code. Could you show me the correct portions to use with
the prewritten wizard code, or better yet, what the code should exactly be
for the Save_Click event?

Sample from knowledgebase:
custDA.UpdateCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5,
"CustomerID");

custDA.UpdateCommand.Parameters.Add("@CompanyName" , OleDbType.VarChar, 40,
"CompanyName");

OleDbParameter myParm =
custDA.UpdateCommand.Parameters.Add("@OldCustomerI D", OleDbType.Char, 5,
"CustomerID");
myParm.SourceVersion = DataRowVersion.Original;

public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection conn)
{
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
OleDbParameter parm;

// Create the UpdateCommand.

cmd = new OleDbCommand("UPDATE Customers SET CustomerID = @CustomerID,
CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", conn);

cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID");
cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName");

parm = cmd.Parameters.Add("@oldCustomerID", OleDbType.Char, 5,
"CustomerID");
parm.SourceVersion = DataRowVersion.Original;

da.UpdateCommand = cmd;

return da;
}

Thanks,
Pam

推荐答案

嗨Pam,


感谢您的发布。关于这个问题,我是

找到合适的资源来帮助你,我们会尽快更新。


问候,


Steven Cheng

Microsoft在线支持


安全! www.microsoft.com/security(This )发布按原样提供;,

没有保修,也没有授予任何权利。)

Hi Pam,

Thank you for your posting. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)


嗨帕姆,


首先,我想确认一下我对你的问题的理解。从

你的描述,我知道你需要知道最好的方式来支付

使用DataAdapter将新数据发送到数据库。如果有任何

误解,请随时告诉我。


根据您的代码提供,我认为你的代码很好。我们可以改进的唯一

的东西是添加数据绑定到表单,

使新添加的行转到直接使用DataTable。每个

TextBox中的数据将自动填充到新添加的行中。这是一篇关于windows窗体中数据绑定的

文章。

http://msdn.microsoft.com/ library / de ... us / dndotnet / ht

ml / databindingadonet.asp


因为你正在使用DataAdapter向导来生成DataAdapter和

DataSet,我们不需要自己将参数添加到OleDbCommand。

一切都由向导完成。它为您生成了代码。如果你扩展Windows窗体设计器生成代码的区域,你会看到

OleDbCommand带有参数创建。


HTH 。


Kevin Yu

=======

此帖子已提供原样没有保证,也没有赋予

权利。

Hi Pam,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to put
the new data to the database using DataAdapter. If there is any
misunderstanding, please feel free to let me know.

Based on the code you have provided, I think your code is fine. The only
thing that we could improve, is to add data binding to the form, which
makes the newly added row goes to the DataTable directly. And data in each
TextBox will be filled to the newly added row automatically. Here is an
article about data binding in windows forms.

http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml/databindingadonet.asp

Since you''re using the DataAdapter wizard to generate the DataAdapter and
DataSet, we needn''t add the parameters to the OleDbCommand ourselves.
Everything has been done by the wizard. It has generated code for you. If
you expand the region of Windows Form Designer generated code, you will see
the OleDbCommand with parameters created.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


不,这不是我需要的。正如我在帖子中指出的那样,我真正需要的是
是UPDATE代码,而不是添加代码。正如我在最初的帖子中指出的那样,我已经有了b / b
的添加功能,并且很高兴知道如何改进它,但

我真正需要的是更新代码,使用向导

写的代码,用于保存按钮。


正如我在初始帖子中指出的,我确实有数据绑定文本字段

已经正确设置并且它正在为我的添加表单正常工作。


其次,就我所知,你给我的参考仅显示如何将
绑定到文本字段以显示字段。它没有说明如何添加,更新或删除文本字段中的数据。


请,我真的需要你来帮助我,因为它是将完成的项目交给我的客户的
的最后一步。我一直在等待周日完成项目的

,所有这一切都基于

更新表格上的保存按钮开始工作。

谢谢,

Pam

Kevin Yu [MSFT]"写道:
No, that is not what I need. As I indicated in my post, what I really need
is the UPDATE code, not add code. As I indicated in my initial post, I have
the add capabilities working, and it is nice to know how to improve it, but
what I really need is the update code, to work with the code that the Wizards
wrote, for the save button.

As I indicated in my initial post, I do have the Data Binding text fields
already set properly and it is working properly for my add form.

Second, as far as I can tell, the reference you gave me only shows how to
bind to text fields to DISPLAY the fields. It does not say how to add,
update, or delete data in text fields.

PLEASE, I really need you to help me with this since it is the last step in
handing the finished project over to my client. I''ve been waiting since
Sunday to finish the project, all based on getting the Save button on the
Update form to work.

Thanks,
Pam
"Kevin Yu [MSFT]" wrote:
嗨帕姆,

首先,我想确认一下我对你的问题的理解。从您的描述中,我了解您需要知道使用DataAdapter将新数据放入数据库的最佳方法。如果有任何误解,请随时告诉我。

根据您提供的代码,我认为您的代码很好。我们可以改进的唯一事情是添加数据绑定到表单,这使得新添加的行直接转到DataTable。每个
TextBox中的数据将自动填充到新添加的行中。这是一篇关于Windows窗体中数据绑定的文章。

http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml / databindingadonet .asp
由于你使用DataAdapter向导生成DataAdapter和
DataSet,我们不需要自己将参数添加到OleDbCommand。
一切都已经过由向导完成。它为您生成了代码。如果你扩展了Windows Form Designer生成代码的区域,你会看到带有参数的OleDbCommand。

HTH。

Kevin Yu
=======
这个帖子是按原样提供的没有保证,也没有赋予
权利。
Hi Pam,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to put
the new data to the database using DataAdapter. If there is any
misunderstanding, please feel free to let me know.

Based on the code you have provided, I think your code is fine. The only
thing that we could improve, is to add data binding to the form, which
makes the newly added row goes to the DataTable directly. And data in each
TextBox will be filled to the newly added row automatically. Here is an
article about data binding in windows forms.

http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml/databindingadonet.asp

Since you''re using the DataAdapter wizard to generate the DataAdapter and
DataSet, we needn''t add the parameters to the OleDbCommand ourselves.
Everything has been done by the wizard. It has generated code for you. If
you expand the region of Windows Form Designer generated code, you will see
the OleDbCommand with parameters created.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."



这篇关于从文本字段更新oledb数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆