ODBC implements dynamic building and Çeku

Source: Internet
Author: User

Introduction


The first few days to put Itoo on the cloud, then the implementation of dynamic building and Çeku is very important. The first way we think of it is to use ODBC, dynamically create the database and create the table, in fact, this way is the simplest, but also a better way to think of the implementation. All things, from the simple beginning, will be better achieved.


Implementation ideas:


Open databases Interconnect (Open database Connectivity,odbc), divided into three steps, the first step: stitching connectionstring strings, the second step: splicing DML or DDL statements, third step: Execute.


In fact, the most important implementation of the dynamic effect is to use string concatenation, or string as a parameter, we can achieve dynamic effect.


Implementation code:


Create a Createdb class:


<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.configuration;using System.Data;using System.data.sqlclient;using system.linq;using system.reflection;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{public class Createdb {//sqlconnection myconn = new SqlConnection ("server=19        2.168.24.233;integrated security=sspi;data source=cutdb1;user id=sa;password=123456 "); SqlConnection myconn = new SqlConnection ("Data source=192.168.24.233;initial catalog=cutdb1; User Id=sa;        password=123456 ");        Data source={0};initial catalog={1};user id={2};p assword={3};            public bool Creatdatabase (string DatabaseName) {string strdatabase;            Strdatabase = "CREATE DATABASE" + DatabaseName;            SqlCommand mycommand = new SqlCommand (strdatabase, myconn);                try {myconn.open ();                Mycommand.executenonquery ();Console.Write ("DataBase is Created successfully", "MyProgram");            return true; } catch (System.Exception ex) {Console.WriteLine (ex.                ToString (), "MyProgram");            return false;                    } finally {if (myconn.state = = ConnectionState.Open) {                Myconn.close (); }}} public bool CreateTable (string DatabaseName, String TableName) {if (myconn.stat            E = = ConnectionState.Open) {myconn.close ();            } string strtable; strtable = "Use" +databasename+ "CREATE TABLE" + TableName + "(myId Integer CONSTRAINT Pkeymyid PRIMARY KEY," + "MyName CH            AR (+), myAddress CHAR (255), mybalance FLOAT) ";            SqlCommand mycommand = new SqlCommand (strtable, myconn);                try {myconn.open (); Mycommand.executenonquery();                Console.Write ("DataBase is Created successfully", "MyProgram");            return true; } catch (System.Exception ex) {Console.WriteLine (ex.                ToString (), "MyProgram");            return false;                    } finally {if (myconn.state = = ConnectionState.Open) {                Myconn.close ();  }}} public static void Changeconfiguration () {//Read the assembly's configuration file string Assemblyconfigfile = assembly.getentryassembly ().            Location;            string appdomainconfigfile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;            Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none); Gets the appSettings node appsettingssection appSettings = (appsettingssection) config.            GetSection ("appSettings"); Remove name, and then add a new value AppsettinGs.            Settings.remove ("ConnStr");            APPSETTINGS.SETTINGS.ADD ("ConnStr", "123456"); Save the configuration file CONFIG.        Save (); }}}</span>


To create a console program call class:


<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.configuration;using System.Data;using System.data.sqlclient;using system.linq;using system.text;using system.threading.tasks;using WebDemo.Models; namespace consoleapplication1{public    class program    {        static void Main (string[] args)        {            createdb A = new Createdb ();            String strdatabasename = "QMX";            String table = "Student";            A.creatdatabase (strdatabasename);            A.createtable (strdatabasename, table);            Console.read ();}}}    </span>

Here we must remember that we do not use configuration files, In particular, the Web. config file, it can not be re-loaded after you modify it (so far, can be implemented to modify the Web file, but not to let it reload once), in order to avoid, we use the concatenation of string and string type as parameters to pass, it is good to solve this problem, when we want to Çeku only Need to add a SqlHelper class, it can be implemented.


Summarize:


In the first place, we wanted to put the dynamic Çeku repository in our EF framework and hit the nails, so sometimes we need to start from the simplest, and when we think about the principles, we can go on. Always from the simple to start, this is still very useful.



ODBC implements dynamic building and Çeku

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.