学习C#中的数据访问 [英] learning about data access in C#

查看:89
本文介绍了学习C#中的数据访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表单,它将使用datagridview和一些文本框来显然读取和写入数据表。

I am creating a form that will use a datagridview and some textboxes to obviously read and write data to a table.

我不想使用设计器来处理数据绑定我的控件。我想学习通过代码来做到这一点,所以我对对象和类有了更好的认识。

I do not want to use the designer to data bind my controls. I want to learn to do it thru code so I have a better appreciation for the objects and classes.

所以这里有一些我编写的方法来填充数据适配器,设置一个绑定源,绑定datagridview和控件。效果很好。

So here are a few methods I have written that fill a data adapter, set up a binding source, bind the datagridview and controls. Works great.


private void frmProperties_Load(object sender, EventArgs e)
      {
      try
        {

        this.dvgPropertyGrid.DataSource = bSource;
        this.dvgPropertyGrid.AutoGenerateColumns = true;

        BindPropertyControls();

        this.comboSearchField.Items.Add("Property Name");
        this.comboSearchField.Items.Add("Project Number");

        }
      catch (Exception ex)
        {
        MessageBox.Show("Error: " + ex.Message);
        }

      }


    private void BindPropertyControls()
      {
      this.propertyIDTextBox.DataBindings.Add(new Binding("Text",bSource,"PropertyID"));
      this.propertyNameTextBox.DataBindings.Add(new Binding("Text", bSource, "PropertyName"));
      this.projectNumberTextBox.DataBindings.Add(new Binding("Text", bSource, "ProjectNumber"));
      this.checkboxScattered.DataBindings.Add(new Binding("Text", bSource, "ScatteredSite"));
      this.totalBuildingsTextBox.DataBindings.Add(new Binding("Text", bSource, "TotalBuildings"));
      this.totalUnitsTextBox.DataBindings.Add(new Binding("Text",bSource,"TotalUnits"));
      this.commentsTextBox.DataBindings.Add(new Binding("Text",bSource,"Comments"));
      }



    private void GetPropertyData(string selectCommand)
      {
      connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\App\\db.accdb";

      OleDbDataAdapter dAdapter = new OleDbDataAdapter(selectCommand, connString);

      //OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

      DataTable dTable = new DataTable();

      dAdapter.Fill(dTable);

      bSource.DataSource = tblPropertyTableAdapter;
      bSource.DataSource = dTable;
      }

推荐答案

这不是一个Windows窗体开发问题,它是一个ADO问题,所以我把这个帖子移到那里,这里的专家会告诉你更多的专家帮助。

This is not a windows form dev question, it is a ADO question, so I moved this thread there, and the experts here will show you more expert helps.

祝福,


这篇关于学习C#中的数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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