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.