Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Bagaimana cara menjalankan kueri SQL ini menggunakan ADO.NET?

Saya menggunakan modifikasi SQL asli Anda untuk tujuan sampel.

DECLARE @list AS TABLE (Name VARCHAR(20));
INSERT INTO @list(Name)
VALUES ('PROCEDURE'), 
        ('FUNCTION');

SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE in (SELECT Name from @list)

Anda dapat menggunakan Parameter Bernilai Tabel

Berikut adalah contoh modifikasi yang diambil dari Parameter Bernilai Tabel di SQL Server 2008 (ADO. BERSIH)

        using (SqlConnection cnn = new SqlConnection("Your connection string"))
        {

            var tableParam = new DataTable("names");
            tableParam.Columns.Add("Name", typeof(string));
            tableParam.Rows.Add(new object[] { "PROCEDURE" });
            tableParam.Rows.Add(new object[] { "FUNCTION')" });

            var sql = @"DECLARE @list AS TABLE (Name VARCHAR(20))
                       INSERT INTO @list(Name)
                       SELECT Name from @Names;

                       SELECT *
                       FROM INFORMATION_SCHEMA.ROUTINES
                       WHERE ROUTINE_TYPE in (SELECT Name from @list)";

            var sqlCmd = new SqlCommand(sql, cnn);
            var tvpParam = sqlCmd.Parameters.AddWithValue("@Names", tableParam);
            tvpParam.SqlDbType = SqlDbType.Structured;
            tvpParam.TypeName = "dbo.Names";

            cnn.Open();
            using(SqlDataReader rdr = sqlCmd.ExecuteReader() )
            {
                while (rdr.Read())
                    Console.WriteLine(rdr["SPECIFIC_NAME"]);                    

            }



        }

Tapi Anda perlu mendefinisikan tipe dbo.Names sebelum bisa bekerja

Berikut jenis pembuatan SQL

CREATE TYPE dbo.Names AS TABLE 
( Name VARCHAR(Max));

Opsi lainnya adalah menggunakan Parameter XML

 using (SqlConnection cnn = new SqlConnection("Your connection string"))
{



    var sql = @"DECLARE @list AS TABLE (Name VARCHAR(20))
                INSERT INTO @list(Name)
                SELECT t.name.value('.', 'varchar(MAX)')
                FROM   @Names.nodes('/Names/Name') as T(Name);

                SELECT *
                FROM INFORMATION_SCHEMA.ROUTINES
                WHERE ROUTINE_TYPE in (SELECT Name from @list)";

    var sqlCmd = new SqlCommand(sql, cnn);

    var s = new MemoryStream(ASCIIEncoding.Default.GetBytes("<Names><Name>PROCEDURE</Name><Name>FUNCTION</Name></Names>"));


    var xmlParam = new SqlXml(s);

    sqlCmd.Parameters.AddWithValue("@Names", xmlParam);


    cnn.Open();
    using(SqlDataReader rdr = sqlCmd.ExecuteReader() )
    {
        while (rdr.Read())
            Console.WriteLine(rdr["SPECIFIC_NAME"]);                    

    }



}



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL Inner Join Pada Nilai Null

  2. Menyiapkan database SQL Server lokal

  3. Perbaiki SQL Database Recovery Pending Access Masalah Ditolak

  4. Batasan unik hanya pada kolom NON-NULL

  5. Cara Mengenkripsi Tampilan di SQL Server