Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using System.Data.SqlClient;
Using System.Data;
Namespace vote
{
public class DataBase
{
SqlConnection Conn;
Constructing a Connection object
String str = "server=huzewei\\huzewei;database=medicdb;uid=hzw;pwd=198211098";
Public DataBase ()
{
String str = "server=huzewei\\huzewei;database=medicdb;uid=hzw;pwd=198211098";
This.conn = new SqlConnection (str);
}
Open connection
public void Open ()
{
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
}
Close Current Connection
public void Close ()
{
IF (Conn. state = = ConnectionState.Open)
{
Conn. Close ();
}
}
Data Sheet DT
Public DataTable Query_dt (String sql)
{
Close ();
SqlDataAdapter SDA = new SqlDataAdapter (SQL, conn);
DataTable dt=new DataTable ();
Sda. Fill (DT);
return DT;
}
Set up DataSet DS
Public DataSet Query_ds (String sql)
{
Close ();
SqlDataAdapter SDA = new SqlDataAdapter (SQL, conn);
DataSet ds = new DataSet ();
Sda. Fill (DS);
return DS;
}
Number of rows affected
public int update (String sql)
{
Close ();
Open ();
SqlCommand sc = new SqlCommand ();
Sc.commandtext = SQL;
Sc.commandtype = CommandType.Text;
Sc. Connection = conn;
int x = SC. ExecuteNonQuery ();
Close ();
return x;
}
Traversing data in a table
Public SqlDataReader Sdread (String sql)
{
Close ();
SqlDataReader SDR;
Open ();
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
Cmd.commandtext = SQL;
Cmd.commandtype = CommandType.Text;
SDR = cmd. ExecuteReader ();
return SDR;
}
Total number of statistics
public int sum (String sql)
{
Close ();
Open ();
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
Cmd.commandtext = SQL;
Cmd.commandtype = CommandType.Text;
int sum = Convert.ToInt32 (cmd. ExecuteScalar ());
Close ();
return sum;
}
Instantiating a SqlDataAdapter object
Public SqlDataAdapter Sqlda (String sql)
{
SqlDataAdapter SDA = new SqlDataAdapter (SQL, conn);
return SDA;
}
Update changes to Data
public void Tbupdate (SqlDataAdapter SDA, DataTable DT)
{
Sda. Fill (DT);
SqlCommandBuilder SCB = new SqlCommandBuilder (SDA);
Perform the update
Sda. Update (dt. GetChanges ());
Make the DataTable Save updates
Dt. AcceptChanges ();
}
Login Password Encryption
public string Adpwd (string pwd)
{
string S1 = null;
for (int i = 0; i < pwd. Length; i++)
{
S1 + = Pwd[pwd. Length-i-1];
}
return s1;
}
}
}
This article is from the "Trials and hardships" blog, please be sure to keep this source http://carben.blog.51cto.com/8690350/1626608
C #, SQL Server database connection, add, delete, change, check and other operations of the class