The code is as follows:
//need to reference a namespaceusingSystem.Data;usingSystem.Data.SqlClient; classDBHelper {//Define database connection statements, connection statement writing method reference (end of article) Public Static stringConnStr ="Data Source=.;i Nitial catalog=mobiledb;integrated security=true"; //Create a database connection object Public StaticSqlConnection conn =NewSqlConnection (CONNSTR); //Define the DataTable Query method and return to table Public StaticDataTable getdatatable (stringsqlstr) { //Create a Databale objectDataTable table =NewDataTable (); //creating an Adapter objectSqlDataAdapter ADP =NewSqlDataAdapter (SQLSTR, Conn); //Data PopulationADP. Fill (table); returntable; } //define methods to perform additions, deletions and changes Public Static BOOLExecuteNonQuery (stringsqlstr) { //Open a database connectionConn. Open (); //Create a Command object and execute the SQL statementSqlCommand cmd =NewSqlCommand (SQLSTR, conn); intresult =cmd. ExecuteNonQuery (); //Close ConnectionConn. Close (); //Defining return Values returnResult >0; } }
*sql database two connection strings
SQL Server Authentication Login: Server = Server name ;d atabase= Database name ; uid= User name ;p wd= Password ;
Windows Authentication Connection string: serve= server name; database= Database name ; Integrated security=true;
C#winfrom minimalist Dbhelp (database connection operation)