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 :\n{0}\n", thisAdapter.SelectCommand.CommandText);
            SqlCommand updateCommand = thisBuilder.GetUpdateCommand();
            Console.WriteLine("SQL UPDATE Command is :\n{0}\n",updateCommand.CommandText);
            SqlCommand insertCommand = thisBuilder.GetInsertCommand();
            Console.WriteLine("SQL INSERT Command is :\n{0}\n",insertCommand.CommandText);
            SqlCommand deleteCommand = thisBuilder.GetDeleteCommand();
            Console.WriteLine("SQL DELETE Command is :\n{0}\n",deleteCommand.CommandText);
            thisConnection.Close();
            Console.WriteLine("Program finished,Press Enter/Return to continue:");
            Console.ReadLine();
        }
    }
}

--按F5运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值