Add and remove SQL Server in C # (easy to understand)

Source: Internet
Author: User
Tags exception handling

1. Adding references
Using System.Data;
Using System.Data.SqlData;


2, established in the database connection, it is recommended to make it a method, convenient multiple use.
String SqlConnection = "Data Source =" Note 1 ";d atabase = Annotation 2;uid =" NOTE 3 ";p WD =" Note 4 "; (There are many other ways)
can refer to http://www.cnblogs.com/delphinet/archive/2011/06/24/2088765.html


3, frequently used objects are: Sqlconnection,sqladapter,sqlcommand, datasets, DataGrid and DataReader, to Sqlconnection,sqladapter, SqlCommand, Dataset, DataGrid object, instance database operation SQL Categorys table in Northwind (assuming the database is local, the user name of the database access is username, password is 123456):
Let's do the following four implementations


First, query data:
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";//define Database connection parameters
SqlConnection myconnection=new SqlConnection (myconn);//define a data connection instance
SqlCommand mycommand=new SqlCommand ("Select CategoryID, CategoryName, Description from Categories", MyConnection); Define a database Operation directive
SqlDataAdapter Selectadapter=new SqlDataAdapter ();//define a data adapter
selectadapter.selectcommand=mycommand;//defining operation instructions for data adapters
DataSet mydataset=new DataSet ();//define a data set
Myconnection.open ();//Open database connection
SelectAdapter.SelectCommand.ExecuteNonQuery ();//execute database query instruction
Myconnection.close ();//Close Database
Selectadapter.fill (myDataSet);//Populating data sets
Datagrid1.datasource=mydataset;
Datagrid1.databind ();//Populate data tables with data from data sets


Second, add data
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String myinsert= "INSERT into Categories (CategoryName, Description) VALUES ('" +convert.tostring (TextBox2.Text) + "', '" + Convert.ToString (TextBox3.Text) + "')";
SqlCommand mycommand=new SqlCommand (myinsert,myconnection);
try//Exception Handling
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}


Third, modify the data
String Categoryname=textbox2.text;
String Categorydescription=textbox3.text;
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String myupdate= "Update Categories set categoryname= '" +categoryname+ "', description= '" +categorydescription+ "' where Categoryid= "+textbox1.text;
SqlCommand mycommand=new SqlCommand (myupdate,myconnection);
Try
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
Textbox1.text= "";
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}


Iv. deletion of data
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String mydelete= "Delete from Categories where categoryid=" +textbox1.text;
SqlCommand mycommand=new SqlCommand (mydelete,myconnection);
Try
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
Textbox1.text= "";
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}

Add and remove SQL Server in C # (easy to understand)

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.