Take the Users table for example, there are three fields, self-growing number id,int type; name Name,nvarchar type, password Pwd,nvarchar type
First, using System.Data.SqlClient is introduced in vs2005; namespaces
<summary>
Increase
</summary>
<param name= "name" > Name </param>
<param name= "pwd" > Password </param>
<returns></returns>
public int Insert (string name,string pwd)
{
SqlConnection conn = new SqlConnection (@ "Data source=.\sqlexpress;initial catalog=test;integrated security=true");// Initial catalog is followed by your database name, if your SQL Server name is not followed by SQLExpress, then data source=.
Conn. Open ();
String sql = "INSERT into users (NAME,PWD) values (@name, @pwd)";
SqlCommand cmd = new SqlCommand (sql,conn);
SqlParameter Parn = new SqlParameter ("@name", name);
Cmd. Parameters.Add (Parn);
SqlParameter parp = new SqlParameter ("@pwd", PWD);
Cmd. Parameters.Add (Parn);
int result = cmd. ExecuteNonQuery ();//result receives the number of rows affected, that is, result is greater than 0 to add success
Conn. Close ();
Cmd. Dispose ();
return result;
}
<summary>
Delete
</summary>
<param name= "name" > Name </param>
<param name= "pwd" > Password </param>
<returns></returns>
public int Update (int id)
{
SqlConnection conn = new SqlConnection (@ "Data source=.\sqlexpress;initial catalog=test;integrated security=true");// Initial catalog is followed by your database name, if your SQL Server name is not followed by SQLExpress, then data source=.
Conn. Open ();
String sql = "Delete from users where [email protected]";
SqlCommand cmd = new SqlCommand (SQL, conn);
SqlParameter Parn = new SqlParameter ("@id", id);
Cmd. Parameters.Add (Parn);
int result = cmd. ExecuteNonQuery ();//result receives the number of rows affected, that is, the result is greater than 0, which means the deletion succeeds
Conn. Close ();
Cmd. Dispose ();
return result;
}
<summary>
Modify
</summary>
<param name= "name" > Name </param>
<param name= "pwd" > Password </param>
<returns></returns>
public int Insert (string name, string Pwd,int ID)
{
SqlConnection conn = new SqlConnection (@ "Data source=.\sqlexpress;initial catalog=test;integrated security=true");// Initial catalog is followed by your database name, if your SQL Server name is not followed by SQLExpress, then data source=.
Conn. Open ();
String sql = "Update users set [email protected],[email protected] where [email protected]";
SqlCommand cmd = new SqlCommand (SQL, conn);
SqlParameter Parn = new SqlParameter ("@name", name);
Cmd. Parameters.Add (Parn);
SqlParameter parp = new SqlParameter ("@pwd", PWD);
Cmd. Parameters.Add (Parn);
SqlParameter pari = new SqlParameter ("@id", id);
Cmd. Parameters.Add (Pari);
int result = cmd. ExecuteNonQuery ();//result receives the number of rows affected, that is, the result is greater than 0, which means the modification succeeds
Conn. Close ();
Cmd. Dispose ();
return result;
}
<summary>
Inquire
</summary>
<returns></returns>
Public DataTable Select ()
{
SqlConnection conn = new SqlConnection (@ "Data source=.\sqlexpress;initial catalog=test;integrated security=true");// Initial catalog is followed by your database name, if your SQL Server name is not followed by SQLExpress, then data source=.
Conn. Open ();
String sql = "SELECT * from users";
SqlCommand cmd = new SqlCommand (SQL, conn);
SqlDataAdapter SDA = new SqlDataAdapter (cmd);
DataTable dt = new DataTable ();
Sda. Fill (DT);
Conn. Close ();
Cmd. Dispose ();
return DT;
}
After the method is written, here is an example of a query, drag a DataGridView in the form, and then in the Load method
private void Form1_Load (object sender, EventArgs e)
{
Datagridview1.datasource = Select ();
}
In this case, the data will be displayed in the DataGridView.
Examples of C # linked database additions and deletions