Drug management system (storeroom management) database connection code +

 

 

--AdminDal.cs
a using System;
a using System.Collections.Generic;
a using System.Linq;
a using System.Text;
a using the System.Data; // contains various types of data objects Ado.Net;
a using the System.Data.SqlClient; / / includes access to all kinds of objects required Server SQL;
a using the System.Configuration;

Drug Information Management System namespace
{
public static class AdminDal
{
public static int SelectCount (the Admin ADMIN)
{
the SqlConnection the sqlConnection the SqlConnection new new = (); // SQL statement and instantiate connection;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings [ "the Sql"]. ToString (); // read from the configuration manager App.config connection string;
the SqlCommand sqlCommand1 sqlConnection.CreateCommand = (); // call connection method CreateCommand SQL creating SQL command; the command will bind SQL connection;
sqlCommand1 .CommandText = "adm_selectAdminCount"; // specify the SQL command text commands; command text for the stored procedure name;
sqlCommand1.CommandType = CommandType.StoredProcedure; type // SQL command set stored procedures;
sqlCommand1.Parameters.AddWithValue ( "@ ANo ", admin.ANo); // parameter SQL command to add the name of a set of parameters, value;
sqlCommand1.Parameters.AddWithValue (" @ Pwd ", ADMIN.pwd);

sqlConnection.Open (); // Open SQL connection;
int the adminCount = (int) sqlCommand1.ExecuteScalar (); // call to SQL commands ExecuteScalar method to execute the command, and receiving a single result (i.e., scalar);
SQLConnection.close () ; // Close SQL connection;
return the adminCount; // returns the number of users;
}
public static int the Insert (the Admin ADMIN)
{
the SqlConnection the sqlConnection the SqlConnection new new = (); // SQL statement and instantiate connection;
sqlConnection.ConnectionString =
the ConfigurationManager. ConnectionStrings [ "Sql"] ToString ( );. // configuration manager reads the connection string from the App.config;
the sqlCommand sqlCommand sqlConnection.CreateCommand = (); // call connection method CreateCommand SQL creating SQL command; the command to bind the SQL connection;
sqlCommand.CommandText = "admin_insertadmin"; // specify the SQL command text commands; command text for the stored procedure name;
sqlCommand.CommandType = CommandType.StoredProcedure; type // SQL command set stored procedures;
sqlCommand.Parameters.AddWithValue ( "@ ANo", admin.ANo ); // Parameter SQL command to add the name of a set of parameters, value;
sqlCommand.Parameters.AddWithValue ( "@ pwd", admin.pwd);
the SqlCommand.Parameters .AddWithValue ( "@ AName", admin.AName);
sqlCommand.Parameters.AddWithValue ( "Tel @", admin.tel);
SqlConnection.Open (); // open SQL connection;
int rowAffected = 0; // declare the whole variable for storing the number of rows affected
try // attempt;
{
rowAffected SqlCommand.ExecuteNonQuery = (); // call to SQL commands ExecuteNonQuery Run method, data is written to the database, and returns the number of rows affected;
}
catch (SqlException sqlEx) // capture SQL exception;
{
IF (sqlEx.Number == 2627) // if the number is 2627 anomaly, the violation of the integrity of entities, i.e., the primary key is inserted into the duplicate records;
{
admin.IsDuplicate to true = ; // similar user exists;
}
the else
{
the throw sqlEx;
}
}
Return rowAffected; // returns the number of rows affected;
}
}
}
--ClientDal.cs
the using the System;
the using the System.Collections.Generic;
the using the System.Linq;
the using the System.Text;
the using the System.Data.SqlClient; // comprising access to all types of objects required Server SQL;
a using the System.Configuration;
a using the System.Data;

Drug Information Management System namespace
{
public class ClientDal
{
public static int the Insert (Client Client)
{
the SqlConnection the sqlConnection the SqlConnection new new = (); // SQL statement and instantiate connection;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings [ "the Sql"] the ToString. (); // read from the configuration manager App.config connection string;
the sqlCommand sqlCommand sqlConnection.CreateCommand = (); // call connection method CreateCommand SQL to create SQL commands; the command will bind SQL connections;
sqlCommand. CommandText = "cli_insertClient"; // specify the SQL command text commands; command text for the stored procedure name;
sqlCommand.CommandType = CommandType.StoredProcedure; type // SQL command set stored procedures;
sqlCommand.Parameters.AddWithValue ( "@ CNo ", client .CNo); // parameter SQL command to add the name of a set of parameters, value;
sqlCommand.Parameters.AddWithValue (" @ CName ", Client .CName);
sqlCommand.Parameters.AddWithValue ( "@ CTEL", client.CTel);
sqlCommand.Parameters.AddWithValue ( "@ CAddress", client.CAddress);
sqlCommand.Parameters.AddWithValue ( "@ Sex", client.sex);
sqlCommand. Parameters.AddWithValue ( "@ RegistDate", client.RegistDate);
sqlCommand.Parameters.AddWithValue ( "@ Birthday", client.Birthday);
sqlCommand.Parameters.AddWithValue ( "@ minsurance", client.minsurance);
// sqlCommand. Parameters.AddWithValue ( "@ Pinyin", client.pinyin);
of the SQLConnection.open (); // open the SQL connection;
int rowAffected = 0; // declare an integer variable to hold the number of rows affected
try // try;
{
rowAffected SqlCommand.ExecuteNonQuery = ();// Call method ExecuteNonQuery SQL commands to execute the command, to write data to the database, and returns the number of rows affected;
}
the catch (the SqlException sqlEx) // capture SQL exception;
{
IF (sqlEx.Number == 2627) // if the number is 2627 anomaly, the violation of the integrity of entities, i.e., the primary key is inserted into the duplicate records;
{
client.IsDuplicate = to true; // similar user exists;
}
the else
{
the throw sqlEx;
}
}
return rowAffected; // returns the number of rows by impact;
}
// public static the SqlDataReader the Select (Client Client)
// {
// the SqlConnection the sqlConnection the SqlConnection new new = (); // SQL statement and instantiate connection;
// = sqlConnection.ConnectionString
// ConfigurationManager.ConnectionStrings [ "the Sql"] the ToString ();. // read from the configuration manager App.config connection string;
// = the SqlCommand sqlCommand1 sqlConnection.CreateCommand (); // call to SQL connection CreateCommand way to create a SQL command; the command will bind the SQL connection;
//sqlCommand1.CommandText = "cli_selectClient"; // specify the SQL command text commands; command text for the stored procedure name;
//sqlCommand1.CommandType = CommandType.StoredProcedure; // type of SQL command set stored procedures;
//sqlCommand1.Parameters.AddWithValue("@CNo ", Client .CNo); // set of parameters to SQL command to add parameters name, value;
//sqlCommand1.Parameters.AddWithValue("@CName ", Client .CName);
//sqlCommand1.Parameters.AddWithValue("@CTel", client.CTel);
//sqlCommand1.Parameters.AddWithValue("@ CAddress ", client.CAddress);
//sqlConnection.Open (); // open SQL connection;
// sqlCommand1.ExecuteReader the SqlDataReader SqlDataReader = ();
// the while (SQLDataReader.Read ())
// {
// String CNO SqlDataReader = [ "@ CNo"] the ToString ();.
// String SqlDataReader CNAME = [ "@ CName"] the ToString ();.
. // String CTEL SqlDataReader = [ "@ CTEL"] the ToString ();
. // String caddress SqlDataReader = [ "@ CAddress"] the ToString ();

//}
//sqlDataReader.Close ();
//sqlConnection.Close (); // Close SQL connection;
// return SqlDataReader; // Returns The number of users;
//}

}
}
--SqlDBHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace 药品信息管理系统
{
class SqlDBHelper
{
public static string connString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
public static string ConnectionString
{
get { return connString; }
set { connString = value; }
}
}
}

 

Guess you like

Origin www.cnblogs.com/xxnzmy/p/12397606.html