Accessing an Access database (polymorphic when there are multiple databases)

Source: Internet
Author: User
Tags connectionstrings

If you want to write a stand-alone MIS, small web site, and so on the database performance requirements of the system, and do not want to install SQL Server, you can use Access (MDAC), as long as an MDB file.
Use Access to create an MDB file and build a table.
OleDbConnection, OleDbCommand ..., usage is about the same as SQL Server. and SQL Server are different:
Some complex SQL functions, syntax access is not supported.
The parameterized query does not use the @name placeholder, but instead uses the?, New OleDbParameter ("?", GUID. ToString () to add OleDbParameter in the order of the SQL statements.
Name and password are reserved words, preferably not used as column names, with the words added []

    String str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;    String pName = configurationmanager.connectionstrings["ConnStr"]. ProviderName;    IDbConnection Conn;    if (pName = = "SQL Server")    {    conn = new SqlConnection (str);    }    else if (PName = = "Access")    {    conn = new OleDbConnection (str);    }    else    {    MessageBox.Show ("Invalid providername!  ");    return;    }    using (conn)    //polymorphism, no one knows whether Conn is SQL or access    {      if (conn). state = = connectionstate.closed)      {        Conn. Open ();      }          using (IDbCommand cmd = conn. CreateCommand ())      {        cmd. CommandText = "Insert into T_person (name,age) Values ('" + txtName.Text + "'," + Convert.ToInt32 (txtage.text) + ")";        if (Convert.ToInt32 (cmd). ExecuteNonQuery ()) > 0)        {        MessageBox.Show ("added success!) ");        }      }    }

Accessing an Access database (polymorphic when there are multiple databases)

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.