C # Connection database and data binding

Source: Internet
Author: User

C # Connection database and data binding

String constr =@ "server= (local)/netsdk;" + "INTEGRATEDSECURITY=SSPI;" + "Database=northwind;"

String sql= "SELECT * from Employees";

SqlConnection conn=new SqlConnection (Constr);

SqlDataAdapter da = new SqlDataAdapter (Sql,conn);

DataSet ds = new DataSet ();

Da.fill (ds, "Employees");

TEXTBOX1.DATABINDINGS.ADD ("Text", ds, "Employees.firstname");

TEXTBOX2.DATABINDINGS.ADD ("Text", ds, "Employees.lastname");

Complex Data binding:

such as: Binding to the DataGrid1 control: datagrid1.setdatabinding (ds, "Employees");

Or: DataGrid1.DataBindings.Add ("DataSource", ds, "Employees");

Can also be written as:

Binding newbind = new Dinding ("Text", ds, "Employees.firstname");

TEXTBOX1.DATABINDINGS.ADD (Newbind);

To synchronize controls with a data source:

First, just like the previous one, connect the database well,

Then drag the dataset onto the form from the Data tab and select the Second Single-line button (vntyped dataset)

BindingManagerBase Bmanager;

Form1_Load Event:

Connect to Library ...

SqlDataAdapter da = new SqlDataAdapter (Sql,conn);

Da.fill (DataSet1, "employess");

Bmanager = this. Bindingcontext[dataset1, "employess"];

Next button, function: Display next record

Bmanager.position +=1;

Back button:

Mmanager.position-=1;

To bind to an array:

string[] cardetails = new string[]{"Mercedez", "Black", "E320", "1999", "$1million"};

TEXTBOX1.DATABINDINGS.ADD ("Text", cardetails,null);

To traverse an array:

BindingManagerBase Bmanager = this. Bindingcontext[cardetails,null];

Bind to Datasheet: Add a DataSet and select no type.

Da.fill (DataSet1, "Employees");

DataTable myTable = dataset1.tables["Employees"];

Listbox1.datasource = myTable;

Listbox1.displaymember = "FirstName";

TEXTBOX1.DATABINDINGS.ADD ("Text", MyTable, "FirstName");

TEXTBOX2.DATABINDINGS.ADD ("Text", MyTable, "LastName");

To bind to a data view:

Da.fill (DataSet1, "Employees");

DataTable myTable = DataSet1. tables["Employees"];

DataView dv = new DataView (myTable, "country= ' UK '", "FirstName", dataviewrowstate.currentrows);

This indicates the name column of all employees who live in the UK from the table in the DataTable object.

TEXTBOX1.DATABINDINGS.ADD ("Text", DV, "FirstName");

TEXTBOX2.DATABINDINGS.ADD ("Text", DV, "LastName");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.