The adonet basic example shares the of adonet connection to the database

  • 2020-06-15 10:07:28
  • OfStack

adonet basic example sharing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace ADONET basis  {
    /// <summary>
    /// Window1.xaml  Interaction logic of 
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ////using()  Automatically closes the database and recyles the resources. 
            ////SqlConnection Object to establish a connection to a database. 
            //using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myself;User Id=sa;Password=123;"))
            //{
            //    conn.Open();// Open the connection 
            //    // By connecting, create 1 Objects that issue commands to a database SqlCommand
            //    using (SqlCommand cmd = conn.CreateCommand())// Release resources. 
            //    {
            //        //CommandText For execution SQL The statement of 
            //        cmd.CommandText = "Insert into student( Student id , The name ) values(110,' zhang 5')";
            //        //ExecuteNonQuery1 Generally used to execute Update Delete Insert  Statements. 
            //        cmd.ExecuteNonQuery();// Perform the above SQL Statements. 
            //    }
            //}

            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "select count(*) from student where  Admission results <570";
                    //cmd.CommandText = "select count(*) from student where  Admission results <570";
                    ////ExecuteScalar1 Used to perform have and only 1 line 1 The column returns a value SQL Statements. 
                    //int i = (int)cmd.ExecuteScalar();
                    //MessageBox.Show(i+" Human achievement less than 570 points ");
                    cmd.CommandText = "Insert into T_Student(Name,Age) output inserted.Id values(' Zhang gu ',18);";
                    long i = (long)cmd.ExecuteScalar();
                    MessageBox.Show("Id for "+i);
                }
            }
            MessageBox.Show(" completes ");
        }
    }
}

Related articles: