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");