C #, SQL Server database connection, add, delete, change, check and other operations of the class

Source: Internet
Author: User

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

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.