C # database connection display

Source: Internet
Author: User

Private void getdata (string selectcommand, string connectionstring, int flag)
{
Sqldataadapter da = new sqldataadapter (selectcommand, connectionstring );

Dataset DS = new dataset ();
Da. Fill (DS );
This. datagridview1.datasource = Ds. Tables [0]; // you need to specify a specific table Ds. Tables [0] when using dataset directly.
}

Private void getdata (string selectcommand, string connectionstring)
{
Try
{
// Create a new data adapter based on the specified query.
Sqldataadapter dataadapter = new sqldataadapter (selectcommand, connectionstring );
// Create a command builder to generate SQL update, insert, and
// Delete commands Based on selectcommand. These are used
// Update the database.
Sqlcommandbuilder commandbuilder = new sqlcommandbuilder (dataadapter );

// Populate a new data table and bind it to the bindingsource.
Datatable table = new datatable ();
// Table. locale = system. Globalization. cultureinfo. invariantculture;

// The following three rows are equivalent to the result: This. datagridview1.datasource = Ds. Tables [0];
Dataadapter. Fill (table );
Bindingsource1.datasource = table;
Datagridview1.datasource = bindingsource1;

// Resize the datagridview columns to fit the newly loaded content.
// Datagridview1.autoresizecolumns (
// Datagridviewautosizecolumnsmode. allcellsexceptheader );
// Datagridview1.show ();
Label1.text = "bingdingsource -----" + Table. Columns [5]. tostring ();
}
Catch (sqlexception)
{
MessageBox. Show ("to run this example, replace the value of the" +
"Connectionstring variable with a connection string that is" +
"Valid for your system .");
}

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.