Three common code

Source: Internet
Author: User
Generic code Public SqlConnection connection_database (string server,string database,string userid,string password)
{
String Str_con;
str_con= "server=" +server+ ";" + "database=" +database+ ";" + "User id=" +userid+ ";" +"

Password= "+password;
Try
{
SqlConnection sqlcon=new SqlConnection (Str_con);
Sqlcon. Open ();

return Sqlcon;
}
Catch
{
MessageBox.Show ("The database connection is unsuccessful!) please fill in the database settings correctly", "error", MessageBoxButtons.

Ok,messageboxicon.stop);
Application.exit ();//Application Exits
return null;
}

}
Public DataView dvlist (string logstring,string tablename)//Returns a view of the corresponding table.
{
if (sqlconn. state = = connectionstate.closed)
Sqlconn. Open ();
SqlDataAdapter mycommand = new SqlDataAdapter (logstring, sqlconn);
DataSet ds = new DataSet ();
Try
{
Mycommand.fill (ds, "+tablename+");
}
Catch
{
MessageBox.Show ("error");
}
DataView Dv = new DataView ();
Dv = ds. tables["+tablename+"]. DefaultView;
return Dv;
}

public void Execute_sql (string sql,sqlconnection sqlconn)/Universal Execute SQL function
{
Open a database connection
if (sqlconn. state = = ConnectionState.Open)
Sqlconn. Close ();
Sqlconn. Open ();
SqlCommand cmd = new SqlCommand (sql, sqlconn);
Try
{
Cmd. ExecuteNonQuery ();
}
catch (SqlException AE)
{
MessageBox.Show (AE. Message.tostring ());
}
Cmd. Dispose ();
}
public void Show_listview_data (string select_str,string tablename,listview LV)/Universal return

tree view function
{
Lv=new ListView ();
Lv. Clear ();
This.sqldataadapter1=new SqlDataAdapter (Select_str,this.sqlconn);
SqlDataAdapter1.Fill (dataSet21, "+tablename+");
Add Column Name
for (int i=0;i<dataset21.tables["+tablename+"]. columns.count;i++)
{
Lv. Columns.Add (dataset21.tables["+tablename+"). Columns[i]. ColumnName.

ToString (), 90,horizontalalignment.left);

}
Add rows (Database data)
for (int j=0;j<dataset21.tables["+tablename+"]. rows.count;j++)
{
ListViewItem ITEM=LV. Items.Add (dataset21.tables["+tablename+"). Rows[j][0].

ToString ());
Item. text=dataset21.tables["+tablename+"]. Rows[j][0]. ToString ();
for (int i=1;i<dataset21.tables["+tablename+"]. columns.count;i++)
Item. SubItems.Add (dataset21.tables["+tablename+"). Rows[j][i].

ToString ());
}
Lv. View=view.details;


}

}



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.