C # simple way to connect to SQL database _c# tutorial

Source: Internet
Author: User
Tags readline

The example in this article describes the method of C # Simple connection to the SQL database. Share to everyone for your reference, specific as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Database Operation Object Library using System.Data;
Using System.Data.SqlClient;
Using Worddic;
      Namespace TestDB {class Program {static void Main (string[] args) {//char[] array = new CHAR[2];
      string [] A = new string[2];
      Fileop words = new Fileop ();
      Words.openfile ("Worddic.txt");
      Words.getwords (ref a);
      /* Console.WriteLine ("Please input data Source:");
      String data_source = Console.ReadLine ();
      Console.WriteLine ("Please enter initial Catalog:");
      String initial_catalog = Console.ReadLine ();
      Console.WriteLine ("Please enter User id:");
      String user_id = Console.ReadLine ();
      Console.WriteLine ("Please enter pass word:");
      String pword = Console.ReadLine (); Connection string strconn = "Data source=" +data_source+; Initial catalog= "+initial_catalog+"; User id= "+user_id+"; password= "+pword+" "//yourpwd replace the sa account password set for you */string strconn =" Data Source=hyper-v-win2003\\sqlsrv2005;initia LCatalog=mytest; User Id=sa;
      Password=sa ";
      SqlConnection conn = null;
      SqlCommand SQLCMD = null;
        try {//Create connection Object conn = new SqlConnection (strconn); Open the database connection conn.
        Open (); Create TRANSAC SQL Command object SQLCMD = conn.
        CreateCommand (); Creating a table statement//sqlcmd.commandtext = "CREATE Table wordlist" (wrongwords varchar (), rightwords varchar (), sign char (20)
        ,)";
        Sqlcmd.executescalar (); while (a[0]!= null) {Sqlcmd.commandtext = INSERT into wordlist (wrongwords,rightwords,sign) VALUES (' + a[0] +
          "', '" + a[1] + "', 1";
          Sqlcmd.executescalar ();
          A[0] = null;
          A[1] = null;
        Words.getwords (ref a);
        } words.fileclose ();
        Console.WriteLine ();
      Print All Records} catch (SqlException e) {Console.WriteLine (e.message); finally {Conn.
      Close ();
 Console.WriteLine ("End of program, press any key to exit");     Console.readkey ();

 }
  }
}

Read more about C # Interested readers can view the site topics: "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # A summary of thread usage tips for programming

I hope this article will help you with C # programming.

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.