Home>

Use the app.config configuration file to encapsulate the connection string,Easy to reuse

--->Add app.conifg configuration file

--->add:connectionstring:

--->Add reference

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <startup>
 <supportedruntime version="v4.0" sku=". Netframework, version=v4.5" />
 </startup>
 <connectionstrings>
 <add name="constr" connectionstring="data source=.;initial catalog =;user id =;password =" />
 </connectionstrings>
</configuration>

Encapsulate a sqlhelper class for easy use

using system.configuration;
using system.data;//datset..table sqldataadapter
using system.data.sqlclient;//sqlconnection command datareader
namespace common
{
 public class sqlhelper
 {
 //connection string
 //1, add references 2, import namespaces in order to use configurationmanager
 private static string constr=configurationmanager.connectionstrings ["constr"]. connectionstring;
 //Add, delete, change and check
 //Find data executescalar () returns the first row and first column executereader () datatable
 ///<summary>
 ///return datatable
 ///</summary>
 ///<param name="sql">The SQL statement used</param>
 ///<param name="param">variable, you can pass parameters or not parameters
 ///<returns></returns>
 public static datatable executedatatable (string sql, params sqlparameter [] param)
 {
 datatable dt=new datatable ();
 using (sqlconnection con=new sqlconnection (constr))
 {
 using (sqldataadapter adapter=new sqldataadapter (sql, con))
 {
  //Add parameters
  adapter.selectcommand.parameters.addrange (param);
  //1. Open the link,If the connection is not open,Then it opens for you;
If open,Never mind
  //2. Execute the SQL statement and read the database
  //3.sqldatareader, fill the read data into the memory table
  adapter.fill (dt);
 }
 }
 return dt;
 }
 ///<summary>
 ///execute the query,Return to first row first column
 ///</summary>
 ///<param name="sql"></param>
 ///<param name="param"></param>
 ///<returns></returns>
 public static object executescalar (string sql, params sqlparameter [] param)
 {
 object o=null;
 using (sqlconnection con=new sqlconnection (constr))
 {
 using (sqlcommand cmd=new sqlcommand (sql, con))
 {
  cmd.parameters.addrange (param);
  con.open ();
  o=cmd.executescalar ();
 }
 }
 return o;
 }
 ///<summary>
 ///execute the query,Return sqldatareader object
 ///</summary>
 ///<param name="sql"></param>
 ///<param name="param"></param>
 ///<returns></returns>
 public static sqldatareader executereader (string sql, params sqlparameter [] param)
 {
 sqldatareader reader=null;
 using (sqlconnection con=new sqlconnection (constr))
 {
 using (sqlcommand cmd=new sqlcommand (sql, con))
 {
  cmd.parameters.addrange (param);
  con.open ();
  reader=cmd.executereader ();
 }
 }
 return reader;
 }
 ///<summary>
 ///Perform additions, deletions, and changes,Returns the number of affected rows
 ///</summary>
 ///<param name="sql"></param>
 ///<param name="param"></param>
 ///<returns></returns>
 public static int executenonquery (string sql, params sqlparameter [] param)
 {
 int n=-1;
 using (sqlconnection con=new sqlconnection (constr))
 {
 using (sqlcommand cmd=new sqlcommand (sql, con))
 {
  cmd.parameters.addrange (param);
  con.open ();
  n=cmd.executenonquery ();
 }
 }
 return n;
 }
 }
}

c#using three use methods:/go.php?id=102855&s=a

c#namespace:is to prevent duplicate names.

For example, you can have student classes in two different namespaces.

This namespace scope allows you to organize your code and gives you a way to create globally unique types.

The usage of sqlparamater in c#:/go.php?id=101015&s=a

The following is the application of sqlhelper and combobox to show the province-city linkage:

public partial class form1:form
 {
 public form1 ()
 {
 initializecomponent ();
 }
 private void form1_load (object sender, eventargs e)
 {
 //When the application loads, go to the database to find the provincial data,Give cbopro
 datatable dt=sqlhelper.executedatatable ("select * from promary");
 //Use the returned datatable as the data source of cbopro
 //Let cbopro display the value of the field proname,Usually shown to customers
 cbopro.displaymember="proname";
 //Let valuememberid be bound to the corresponding value,The binding handler identifier is for the programmer.
 cbopro.valuemember="proid";
 cbopro.datasource=dt;
 }
 private void cbopro_selectedindexchanged (object sender, eventargs e)
 {
 //Before
 //messagebox.show (cbopro.text);//Get the selected text in cbo
 //messagebox.show(cbopro.selectedvalue.tostring());//Get the associated data
 //string sql="select * from city where proid =" + cbopro.selectedvalue.tostring ();
 //SQL statement with parameters
 string sql="select * from city where proid [email protected]";
 //Prepare a sql parameter
 sqlparameter p=new sqlparameter ("@ proid", cbopro.selectedvalue.tostring ());
 //Set the data to be displayed in cbocity
 cbocity.displaymember="cityname";
 //Dataset queried according to the SQL statement
 cbocity.datasource=sqlhelper.executedatatable (sql, p);
 }
 }
c
  • Previous C # common protocol implementation template and FixedSizeReceiveFilter example (introduction to SuperSocket)
  • Next Talking about C # Class Inheritance