程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#顯示SQL語句格式

C#顯示SQL語句格式

編輯:C#入門知識

C#顯示SQL語句格式


--SQL SERVER生成測試環境:

 

Create database Test; 
go
USE [Test]
GO

if OBJECT_ID('Tab','U') is not null
	drop table Tab
go
CREATE TABLE [dbo].[Tab](
	[ID] [int] NOT NULL  CONSTRAINT [PK_Tab] PRIMARY KEY CLUSTERED ,
	[name] [sysname] NOT NULL
)


--打開Visual Studio—創建項目—選擇【控制台應用程序】

 

 

#region Using Directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion

namespace TestShowSql
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection thisConnection = new SqlConnection(@Server=(Local);Integrated Security=True;Database=Test);
            //thisConnection.Open();
            SqlDataAdapter thisAdapter = new SqlDataAdapter(SELECT ID,NAME FROM TAB,thisConnection);
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
            Console.WriteLine(SQL Select Command is :
{0}
, thisAdapter.SelectCommand.CommandText);
            SqlCommand updateCommand = thisBuilder.GetUpdateCommand();
            Console.WriteLine(SQL UPDATE Command is :
{0}
,updateCommand.CommandText);
            SqlCommand insertCommand = thisBuilder.GetInsertCommand();
            Console.WriteLine(SQL INSERT Command is :
{0}
,insertCommand.CommandText);
            SqlCommand deleteCommand = thisBuilder.GetDeleteCommand();
            Console.WriteLine(SQL DELETE Command is :
{0}
,deleteCommand.CommandText);
            thisConnection.Close();
            Console.WriteLine(Program finished,Press Enter/Return to continue:);
            Console.ReadLine();
        }
    }
}

--按F5運行結果:

 

/

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved