How does C realize adding deleting modifying and checking sql and server database
- 2021-07-18 08:51:21
- OfStack
A special implementation of sql server database addition, deletion, modification and search, and return the results of the query into a table and other functions, sharing the code as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;// No. 1 1 Step: Reference to sql Related namespace
using System.Data;// Namespace of reference table
// Line comment /// Paragraph comment
/// <summary>
/// No. 1 2 Step: Delete the namespace so that we don't have to create it in future development 1 A sqlhelper Class and the namespace that references it. Implement direct call
/// </summary>
// No. 1 3 Step: In class Add before it 1 A public Achieve that we can dal To call it anywhere in.
//public Called public class It has the greatest access rights. If in class If we add it in front of us, we can call this class at will in the current class library.
public class SqlHelper
{
// No. 1 4 Step: We want to call directly sqlHelper Class, it is not enough to delete the namespace. We also need to set all the methods and fields in it to static.
public static string connstr = "server=.;uid=sa;pwd=sa;database=rj1301";// Common static Connection string
public static int ExecuteScalar(string cmdText, params SqlParameter[] pms)
{
//params: Array of unlimited length
// No. 1 1 Step: Create a database object connection
SqlConnection conn = new SqlConnection(connstr);
// No. 1 2 Step: Open the database
conn.Open();
// No. 1 3 Step: Create database command objects and database query statements
SqlCommand cmd = new SqlCommand(cmdText, conn);
// No. 1 4 Step: Create Parameter Objects
//cmd.Parameters.Add(pms);// If we put it directly here pms Array, then cmd All the things added are SqlParameter Instead of specific parameters.
// int[] ii = new int[5] { 1,2,3,4,5 };
if (pms != null)
{
//for (int i = 0; i <pms.Length; i++)
//{
// if (pms[i]!=null)
// {
// cmd.Parameters.Add(pms[i]);
// }
//}
foreach (SqlParameter item in pms)// No. 1 1 Parameters: Your data type // No. 1 2 Parameters are values // No. 1 3 Parameters: in In what what // No. 1 4 Parameters The name of the array
{
if (item != null)
{
cmd.Parameters.Add(item);
}
}
}
// No. 1 5 Step: Return the results
int i = Convert.ToInt32(cmd.ExecuteScalar());
// No. 1 6 Step: Close the database
conn.Close();
// No. 1 6 Step : Return the final result to the outside
return i;
}
public static int ExecuteNonQuery(string cmdText, params SqlParameter[] pms)
{
//params: Array of unlimited length
// No. 1 1 Step: Create a database object connection
SqlConnection conn = new SqlConnection(connstr);
// No. 1 2 Step: Open the database
conn.Open();
// No. 1 3 Step: Create database command objects and database query statements
SqlCommand cmd = new SqlCommand(cmdText, conn);
// No. 1 4 Step: Create Parameter Objects
//cmd.Parameters.Add(pms);// If we put it directly here pms Array, then cmd All the things added are SqlParameter Instead of specific parameters.
// int[] ii = new int[5] { 1,2,3,4,5 };
if (pms != null)
{
//for (int i = 0; i <pms.Length; i++)
//{
// if (pms[i]!=null)
// {
// cmd.Parameters.Add(pms[i]);
// }
//}
foreach (SqlParameter item in pms)// No. 1 1 Parameters: Your data type // No. 1 2 Parameters are values // No. 1 3 Parameters: in In what what // No. 1 4 Parameters The name of the array
{
if (item != null)
{
cmd.Parameters.Add(item);
}
}
}
// No. 1 5 Step: Return the results
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
// No. 1 6 Step: Close the database
conn.Close();
// No. 1 6 Step : Return the final result to the outside
return i;
}
// Return 1 Table
public static DataTable GetTable(string cmdText, params SqlParameter[] pms)
{
//params: Array of unlimited length
// No. 1 1 Step: Create a database object connection
SqlConnection conn = new SqlConnection(connstr);
// No. 1 2 Step: Open the database
conn.Open();
// No. 1 3 Step: Create database command objects and database query statements
SqlCommand cmd = new SqlCommand(cmdText, conn);
// No. 1 4 Step: Create Parameter Objects
//cmd.Parameters.Add(pms);// If we put it directly here pms Array, then cmd All the things added are SqlParameter Instead of specific parameters.
// int[] ii = new int[5] { 1,2,3,4,5 };
if (pms != null)
{
//for (int i = 0; i <pms.Length; i++)
//{
// if (pms[i]!=null)
// {
// cmd.Parameters.Add(pms[i]);
// }
//}
foreach (SqlParameter item in pms)// No. 1 1 Parameters: Your data type // No. 1 2 Parameters are values // No. 1 3 Parameters: in In what what // No. 1 4 Parameters The name of the array
{
if (item != null)
{
cmd.Parameters.Add(item);
}
}
}
//5. SqlDataAdapter Yes .net Used to store the number taken out from the array library in Equivalent to the container in our real life
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//6. To create 1 Adapters Used to accept the container
DataSet ds = new DataSet();
sda.Fill(ds,"aa");
conn.Close();
DataTable dt = ds.Tables["aa"];
return dt;
}
public static SqlDataReader GetReader(string cmdText, params SqlParameter[] pms)
{
SqlConnection conn = new SqlConnection(connstr);
// No. 1 2 Step: Open the database
conn.Open();
// No. 1 3 Step: Create database command objects and database query statements
SqlCommand cmd = new SqlCommand(cmdText, conn);
// No. 1 4 Step: Create Parameter Objects
//cmd.Parameters.Add(pms);// If we put it directly here pms Array, then cmd All the things added are SqlParameter Instead of specific parameters.
// int[] ii = new int[5] { 1,2,3,4,5 };
if (pms != null)
{
//for (int i = 0; i <pms.Length; i++)
//{
// if (pms[i]!=null)
// {
// cmd.Parameters.Add(pms[i]);
// }
//}
foreach (SqlParameter item in pms)// No. 1 1 Parameters: Your data type // No. 1 2 Parameters are values // No. 1 3 Parameters: in In what what // No. 1 4 Parameters The name of the array
{
if (item != null)
{
cmd.Parameters.Add(item);
}
}
}
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);// Take out the data and put it in the form of a cursor sdr
return sdr;
}
}
The above is the implementation code of sql server database addition, deletion and modification, hoping to help everyone's study.