ADO.NET performs simple addition, deletion, and modification of SqlServer

To add, delete, and modify the database, the first thing that comes to mind is the connection string.

Our connection string is composed of "Server = address (local = local); Database = database name; User Id = login user name; Password = password" ; of course, the attributes in the connection string cannot be that way, we The essay is not here for systematic study, the villain is going to write an essay on the connection string.

Then we start looking at the code below

Reference namespace using system.data.SqlClient

  ///  <summary> 
        /// Check
         ///  </ summary> 
        ///  <returns> </ returns> 
        public  void ExecuteReader ( string connectionString) 
        { 
            // sql statement 
            string sqlString = " select * from Employee where Name = @Name " ;
            // Opened in the using block of SqlConnection, which ensures that the resource is closed and released when the code exits. 
            using (SqlConnection sqlConnection = new SqlConnection (connectionString)) 
            { 
                SqlCommand sqlCommand = newSqlCommand (sqlString, sqlConnection);
                 // CommandType = StoredProcedure, IDbCommand.CommandText property should be set to the name of the stored procedure to be accessed.
                // CommandType = TableDirect, IDbCommand.CommandText property should be set to the name of the table to be accessed. Only the .NET Framework data provider for OLE DB supports TableDirect, and multiple table access is not supported when IDbCommand.CommandType is set to TableDirect.
                // CommandType defaults to Text, SQL text command. 
                sqlCommand.CommandType = CommandType.Text; 
                sqlCommand.CommandText = sqlString; 
                sqlCommand.Parameters.AddWithValue ( " @Name " , " zsk " );
                 try
                {
                    sqlConnection.Open();
                    SqlDataReader reader = sqlCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2}",
                            reader[0], reader[1], reader[2]);
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }
            }
        }

The SqlDataReader class here provides a way to read only the incoming stream from the SQL Server database. To create , you must call the ExecuteReader method of the SqlCommand object instead of directly using the constructor.

 public void ExecuteNonQuery(string connectionString)
        { 
        string SqlString = "insert into Employee(Name, Age) values(@Name, @Age)";
            //建立连接
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(SqlString, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@Name","zm");
                sqlCommand.Parameters.AddWithValue ( " @Age " , 21 );
                 try 
                { 
                    sqlConnection.Open (); 
                    /// Return the number of affected rows 
                    int row = sqlCommand.ExecuteNonQuery ();
                     if (row> 0 ) 
                    { 
                        Console.WriteLine ( " Successfully added " ); 
                    } 
                } 
                catch (Exception ex) 
                { 
                    Console.WriteLine (ex.Message); 


                }
            }
        }

The ExecuteNonQuery method of SqlCommand is used in the above code. This method provides the number of affected rows and can be used for most simple additions, deletions, and changes.

ADO.NET also provides a SqlDataAdapter class in the DataSet between SQL Server and act as a bridge to retrieve and save data. SqlDataAdapter provides this bridge by mapping Fill .

 

Guess you like

Origin www.cnblogs.com/Z-ba-S-wei-K/p/12757840.html