Examples of C # linked database additions and deletions

Source: Internet
Author: User

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

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.