ADO.NETの7-Commandオブジェクトを使用してデータベースレコードを検索---ShinePans

3344 ワード

クエリー・データ・パスのデータ数:
コード:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ///     
            string connection =
                "server=  \\SQLEXPRESS;database=db_test;Trusted_Connection=true";
            SqlConnection sc = new SqlConnection(connection);
            sc.ConnectionString = connection;
            try
            {
                sc.Open();  //       
                Console.WriteLine("         !");
                SqlCommand cmd = new SqlCommand("SELECT count(*) FROM db_student", sc);
                int i = (int)cmd.ExecuteScalar();//         
                Console.WriteLine("    {0}   ", i.ToString());
//START:3.          ////////////////////////////////////////////////////////
             /*   SqlCommand cmd = new SqlCommand("UPDATE db_student SET student_grade=99 where student_name=@name", sc);  //  SqlCommand  
                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = " ";
                int i = cmd.ExecuteNonQuery();
                if (i > 0) Console.WriteLine("    !");   */
//END:3.          /////////////////////////////////////////////////////////
//START:1.          ///////////////////////////////////////////////////////
               /* string cmdtext = "DELETE FROM db_student WHERE student_name=@name";
                SqlCommand cmd = new SqlCommand(cmdtext, sc);
                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = " ";
                int i = cmd.ExecuteNonQuery();
                if (i > 0) Console.WriteLine("      !"); */
//END:1.          /////////////////////////////////////////////////////////
//START:2.       ///////////////////////////////////////////////////////////////
             /*   SqlCommand cmd = new SqlCommand();//  SqlCommand  
                cmd.CommandType = CommandType.Text; //        
                cmd.Connection = sc; //      
                cmd.CommandText = 
                    "INSERT INTO db_student(student_name,student_age,student_address,student_grade)VALUES(@name,@age,@address,@grade)";
                //          
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = " ";
                cmd.Parameters.Add("@age", SqlDbType.Int).Value = 19;
                cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = "  ";
                cmd.Parameters.Add("@grade", SqlDbType.Int).Value = 100;
                int i = cmd.ExecuteNonQuery(); //           
                if (i > 0) Console.WriteLine("      "); */  //          
//END:2.       /////////////////////////////////////////////////////////////////
            }
            catch (Exception ex)
            {
                Console.WriteLine("       :{0}", ex.Message);
            }
            finally
            {
                sc.Close();
                Console.WriteLine("        !");
            }
            System.Console.ReadLine();
        }
    }
}

実行結果: