如何将数据从表单输入数据库? [英] Ways to input data from forms to databases?

查看:104
本文介绍了如何将数据从表单输入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只知道两种方法来执行此操作:一种是通过使用带"Insert into ..."命令字符串的oledbcommand或sqlcommand,另一种是直接输入到datagridview并使用dataadapter更新数据库.您能和我分享更多输入数据的方法吗?(例如通过使用数据绑定?).
非常感谢!
我更喜欢看示例代码而不是任何东西!

解决方案

数据绑定不会使用sqlcommands和dataAdapters将数据插入数据库中,我认为这是可以插入数据库的两种方式. /blockquote>

您应该阅读有关 ADO.Net 基础的更多信息.
然后,您应该了解LINQ,如果仍然希望成为一名编码人员,那么您应该了解Entity Framework.

并尝试自己寻找答案,这个主题不是深奥的;)! http://www.youtube.com/watch?v=4kBXLv4h2ig&feature=related

当涉及到ADO.Net时,与数据库之间的数据传输相关的每个动作都需要两件事:
打开连接和DBCommand对象(SqlCommand,OledbCommand,Odbc ...它们都继承自DBCommand类).
DataAdapter是数据表和命令之间的中间人".

数据绑定无非就是将控件上的值与特定DataRow中的数据连接起来,更改完成后,这些更新可以发送到DB或废弃.

首先,您需要数据集(最好使用适配器).
您可以通过以下三种方式将数据绑定到使用者表单:
1.在数据源"窗口中,您只需从数据集中选择表,然后将列表示形式拖放到表单中.这样,IDE会为您完成所有工作.
2.从工具"窗口中将绑定源组件添加到使用者表单,并将其连接到数据集或数据集中的数据表.然后,将表单上的特定控件绑定到绑定源中的值.
3.手工完成所有操作.我不会去那里:).


附录:
我推荐这个食谱:
DoubleClick用于编辑选定的行并显式添加新的行调用(通过按钮或其他方式).
这是简化版本.

首先,您必须设置dataGridView

//this can be done in designer or by hand
myDataGridView.DataSource = myDataSet;
myDataGridView.DataMember = "TableName";
myDataGridView.AllowUserToAddRows = false;
//dataGridView customization (Columns, styles...)comes here

myDataGridView.DoubleClick += new EventHandler(myDataGridView_DoubleClick);



双击事件处理程序可以是这样的:

void myDataGridView_DoubleClick(object sender, EventArgs e)
{
    if(myDataGridView.SelectedRows.Count > 0)
    {
        int rowID = (int)myDataGridView.SelectedRows[0].Cells["IDColumn"].Value;
        MyDataInputForm frm = new MyDataInputForm();
        frm.Data = myDataSet;
        frm.SetEdit(rowID);
        if(frm.ShowDialog() == DialogResult.OK)
        {
            //validation?
            frm.EndEdit();
        }
        else
        {
            frm.CancelEdit();
        }
    }
}



要添加新行,您可以像这样调用(从工具栏,按钮...)方法

void AddNewRow()
{
    DatSet copy = myDatSet.Copy();

    MyDataInputForm frm = new MyDataInputForm();
    frm.Data = copy;
    DataRow dr = copy.Tables["TableName"].NewRow();
    //add initial values to row
    copy.Tables["TableName"].Rows.Add(dr);
    frm.SetEdit((int)dr["IDColumn"]);
    if(frm.ShowDialog() == DialogResult.OK)
    {
        //validation?
        frm.EndEdit();
        myDatSet.Clear();
        myDatSet.Merge(copy);
    }
    else
    {
        frm.CancelEdit();
    }
}



MyDataInputForm继承自IDataEditor< t>.界面

public interface IDataEditor<t>
{
   void CancelEdit();

   void EndEdit();

   void SetEdit(T objectID);

   DataSet Data { get; set; }
}</t>




 //  bindingSource,dataSet和data控件的绑定是在设计器中完成的
公共 部分  MyDataInputForm:表单,IDataEditor< ; int>
{
    公共 MyDataInputForm()
    {
        InitializeComponent();
    }

    公共 无效 CancelEdit()
    {
         .myBindingSource.CancelEdit();
    }

    公共 无效 EndEdit()
    {
         .myBindingSource.EndEdit();
    }

    公共 无效 SetEdit( int  objectID)
    {
         .myBindingSource.Filter = "  + objectID;
        Application.DoEvents();
    }
        
    公共 DataSet数据
    {
        获取
        {
            返回  .dataSet;
        }
        设置
        {
            .dataSet =  .myBindingSource.DataSource =  .myBindingSource.DataMember = " ;
         }
    }
} </  int  >  



您可以将IDataEditor.Data更改为自定义类型的dataSet.
总结的友好建议:
除非您知道自己在做什么或没有大的验证问题,否则不要过多依赖直接使用dataGridView操作数据.

干杯!


I know only two ways to do this: 1 by using oledbcommand or sqlcommand with a command string of "Insert into ..." and the other by inputting directly into a datagridview and update the database using dataadapter. Could you please share with me more ways to input data?(such as by using databinding?).
Thank you so much!
I prefer seeing example codes to anything!

解决方案

databinding does not insert data into database, using sqlcommands and dataAdapters i think are the two ways you can insert into databases.


You should read more about ADO.Net basics.
Then you should learn about LINQ and if you still have desire to be a coder you should learn about Entity framework.

And try to look for answers yourself, this topic isn''t esoteric ;)! http://www.youtube.com/watch?v=4kBXLv4h2ig&feature=related

When it comes to ADO.Net every action related to transfer of data to or from database needs two things:
open connection and DBCommand object(SqlCommand, OledbCommand, Odbc... they all inherit from DBCommand class).
DataAdapter is "middle man" between datatables and commands.

Data binding is nothing more than connecting values on controls with data within particular DataRow, after you are done with changes those updates can be sent to DB or scrapped.

First of all you need data set (preferably with adapters).
You can bind data to the consumer form in three ways:
1. From Data Sources window, you simply choose table from your data set and drag and drop column representations to your form. This way everything is done by IDE for you.
2. You add binding source component to consumer form from Tools window and connecting it to the data set or data table within data set. Then you bind particular controls on your form to values from binding source.
3. Do all by hand. I wouldn''t go there :).


Addendum:
I recommend this recipe:
DoubleClick for editing selected row and explicit Add new row call (via button or something).
This is simplified version.

First you must set your dataGridView

//this can be done in designer or by hand
myDataGridView.DataSource = myDataSet;
myDataGridView.DataMember = "TableName";
myDataGridView.AllowUserToAddRows = false;
//dataGridView customization (Columns, styles...)comes here

myDataGridView.DoubleClick += new EventHandler(myDataGridView_DoubleClick);



Event handler for double click can be something like this:

void myDataGridView_DoubleClick(object sender, EventArgs e)
{
    if(myDataGridView.SelectedRows.Count > 0)
    {
        int rowID = (int)myDataGridView.SelectedRows[0].Cells["IDColumn"].Value;
        MyDataInputForm frm = new MyDataInputForm();
        frm.Data = myDataSet;
        frm.SetEdit(rowID);
        if(frm.ShowDialog() == DialogResult.OK)
        {
            //validation?
            frm.EndEdit();
        }
        else
        {
            frm.CancelEdit();
        }
    }
}



For adding new row you can call (from toolbar, button...) method like this

void AddNewRow()
{
    DatSet copy = myDatSet.Copy();

    MyDataInputForm frm = new MyDataInputForm();
    frm.Data = copy;
    DataRow dr = copy.Tables["TableName"].NewRow();
    //add initial values to row
    copy.Tables["TableName"].Rows.Add(dr);
    frm.SetEdit((int)dr["IDColumn"]);
    if(frm.ShowDialog() == DialogResult.OK)
    {
        //validation?
        frm.EndEdit();
        myDatSet.Clear();
        myDatSet.Merge(copy);
    }
    else
    {
        frm.CancelEdit();
    }
}



MyDataInputForm inherits from IDataEditor<t> interface

public interface IDataEditor<t>
{
   void CancelEdit();

   void EndEdit();

   void SetEdit(T objectID);

   DataSet Data { get; set; }
}</t>




//bindingSource, dataSet and dataBinding to controls is done in designer
public partial class MyDataInputForm : Form, IDataEditor<int>
{
    public MyDataInputForm ()
    {
        InitializeComponent();
    }        

    public void CancelEdit()
    {
        this.myBindingSource.CancelEdit();
    }

    public void EndEdit()
    {
        this.myBindingSource.EndEdit();
    }

    public void SetEdit(int objectID)
    {
        this.myBindingSource.Filter = "IDColumn=" + objectID;
        Application.DoEvents();
    }
        
    public DataSet Data
    {
        get
        {
            return this.dataSet;
        }
        set
        {
           this.dataSet= value;
           this.myBindingSource.DataSource = value;
           this.myBindingSource.DataMember = "TableName";
         }
    }
}</int>



You can change IDataEditor.Data to your custom typed dataSet.
Friendly advice to conclude this:
Don''t rely to much on directly manipulating data with dataGridView unless you know what are you doing or you don''t have big validating issues.

Cheers!


这篇关于如何将数据从表单输入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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