ADO. An overview of the 5 large objects in net :
1---Connection object : Connect to the database.
(1) Log in with SA
conn = new SqlConnection ("Data source=newland-pc;initial catalog=srr1100u; User id=sa;pwd=123456; " ); Conn. Open ();
(2) using Windows Local Login
New SqlConnection ("Data source=newland-pc;initial catalog=image;integrated security=true;"
2---Command object : Executes the T-SQL statement.
(1) ExecuteNonQuery () method, perform additions and deletions to change the command. Returns the number of rows affected! One
if (comm. ExecuteNonQuery () > 0) {MessageBox.Show (" saved successfully! "); }
(2) The ExecuteReader () method returns a DataReader object. Typically used for string types
Getconn (); string select = " select COUNT (distinct Data) as num from srr1100u " ; Comm = new SqlCommand (select Span style= "color: #000000;" >,conn); SqlDataReader MyData = Comm. ExecuteReader (); Mydata.read (); " number: + mydata[" num ]. ToString ();
(3) The ExecuteScalar () method returns the first line of the column. Can be used for byte[] arrays:
getconn (); stringSelect"select Image from cameraimage where imagename= '" "'"; New SqlCommand (Select, conn); byte[] MyByte = (byte[]) comm. ExecuteScalar ();
(4) sqlparameter[] parameter, which is a dataset mapping that can be used to update large amounts of data at once.
Standard notation:
string Select="Insert Message (Carddata,name,money,tel) VALUES (@CardData, @Name, @Money, @Tel)"; Sqlparameter[] Mysqlparameter=Newsqlparameter[] {NewSqlParameter ("@CardData", Mycarddata),NewSqlParameter ("@Name", Dataname),NewSqlParameter ("@Money", Datamoney),NewSqlParameter ("@Tel", Datatel)}; Comm=NewSqlCommand (Select, conn); if(Mysqlparameter! =NULL&& mysqlparameter.length >0) { foreach(SqlParameter Pinchmysqlparameter) {Comm. Parameters.Add (P); } } if(Comm. ExecuteNonQuery () >0) {MessageBox.Show ("Write Success"); }
Simplified notation:
string Select="Insert Message (Carddata,name,money,tel) VALUES (@CardData, @Name, @Money, @Tel)"; Comm=NewSqlCommand (Select, conn); Comm. Parameters.Add (NewSqlParameter ("@CardData", SqlDbType.NVarChar)); Comm. Parameters.Add (NewSqlParameter ("@Name", SqlDbType.NVarChar)); Comm. Parameters.Add (NewSqlParameter ("@Money", SqlDbType.NVarChar)); Comm. Parameters.Add (NewSqlParameter ("@Tel", SqlDbType.NVarChar)); Comm. parameters[0]. Value =Mycarddata; Comm. parameters[1]. Value =Dataname; Comm. parameters[2]. Value =Datamoney; Comm. parameters[3]. Value =Datatel; if(Comm. ExecuteNonQuery () >0) {MessageBox.Show ("Write Success"); }
3---DataAdapter objects : Data adapters, retrieving data from the database, and populating the local data set. at the same time, we can use Dataadapterto update the data back from the dataset to the database.
(1) The Fill method of the DataAdapter, which is used to populate the DataSet with the results of the DataAdapter SelectCommand.
Getconn ();
string select = " select * from Cameraimage where imagename= ' + address + " ' ' ;
Mysqldataadapter = new SqlDataAdapter (sele CT , conn);
myDataSet = new DataSet ();
Mysqldataadapter.fill (myDataSet);
MemoryStream mymemorystream = byte []) mydataset.tables[0 ]. Rows[0 ][2 ]);
4---DataReader object : is a forward-only data stream. Used to retrieve a read-only dataset from the data source and must be connected.
CommandBehavior.CloseConnection property, closing conn connection at the same time
Getconn (); string myselect = " select COUNT (carddata) as num from dbo. Message = new SqlCommand (Myselect, conn); SqlDataReader mysqldatareader = Comm. ExecuteReader (commandbehavior.closeconnection); if (Mysqldatareader. Read ()) {MessageBox.Show (read2[ num ". ToString ()); }
Read. Close ();
5---DataSet object : Is the core of ADO, the equivalent of a local database, which is independent of the database, and the bridge to the remote database connection is the . NET data Provider. It is composed of multiple tables, each of which is a DataTable object.
Getconn (); string Select="SELECT * from Cameraimage where imagename= '"+ Address +"'"; Mysqldataadapter=NewSqlDataAdapter (Select, conn); myDataSet=NewDataSet (); Mysqldataadapter.fill (myDataSet); MemoryStream Mymemorystream=NewMemoryStream ((byte[]) mydataset.tables[0]. rows[0][2]); Tables table label, rows[row [column]
SQL (2)---C # applications