C #-SqlServer connection

Source: Internet
Author: User

C # connecting to a database is not applicable to java in terms of classes. Different databases have different class libraries. Only the connection class of SqlServer is used here.

Public class DbLink {private string config = ConfigurationManager. connectionStrings ["SqlConnection"]. connectionString; // you can directly write the connection string private SqlConnection con; private OracleConnection oracon; private SqlCommand com; private SqlTransaction sqlTra; public DbLink () {con = new SqlConnection (config ); con. open (); com = new SqlCommand (); com. connection = con;} // data retrieval public SqlDataReader select (string SQL) {com. commandText = SQL; SqlDataReader sdr = com. executeReader (); return sdr;} // retrieve public int selectCount (string SQL) {com. commandText = SQL; int count = (int) com. executeScalar (); return count;} // data update/Add/delete public int update (string SQL) {com. commandText = SQL; int num = com. executeNonQuery (); return num;} // enable the public void beginTransaction () {sqlTra = con. beginTransaction (); com. transaction = sqlTra;} // Transaction commit public void commit () {sqlTra. commit () ;}// transaction rollback public void rollback () {sqlTra. rollback ();}}

The connection string is configured in web. config.

  <connectionStrings>    <add name="SqlConnection" connectionString="server=.;database=Temp;uid=sa;pwd=123" providerName="System.Data.SqlClient"/>  </connectionStrings>

 

Related Article

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.