首页 > 数据库技术 > 详细

C#显示SQL语句格式

时间:2015-11-08 19:30:12      阅读:314      评论:0      收藏:0      [点我收藏+]

--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运行结果:

技术分享


版权声明:本文为博主原创文章,未经博主允许不得转载。

C#显示SQL语句格式

原文:http://blog.csdn.net/roy_88/article/details/49718891

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!