C# 某人加上(“;”); 输出=sb.ToString(); } 获取{返回输入;} } 私有字符串输出=string.Empty; 公共字符串输出 { 设置 { 如果(输出==值)返回; 产出=价值; NotifyPropertyChanged(“输出”); } 获取{返回输出;} } } }

C# 某人加上(“;”); 输出=sb.ToString(); } 获取{返回输入;} } 私有字符串输出=string.Empty; 公共字符串输出 { 设置 { 如果(输出==值)返回; 产出=价值; NotifyPropertyChanged(“输出”); } 获取{返回输出;} } } },c#,.net,tsql,C#,.net,Tsql,基于EAKTECAT正则表达式的注释可能是更好的方法 但我已经编写了一个小型独立实用程序 所有的标签看起来都很奇怪,但当我粘贴时,这就是我需要的标签的典型数量 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/wi

基于EAKTECAT正则表达式的注释可能是更好的方法
但我已经编写了一个小型独立实用程序
所有的标签看起来都很奇怪,但当我粘贴时,这就是我需要的标签的典型数量

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        DataContext="{Binding RelativeSource={RelativeSource self}}" 
        Title="SQL Formatter" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TextBox  AcceptsReturn="True" AcceptsTab="True"  
                      Text="{Binding Path=Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </ScrollViewer>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TextBox  Text="{Binding Path=Output, Mode=OneWay}"/>
        </ScrollViewer>
    </Grid>
</Window>

using System.ComponentModel;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
        }
        private string input = "input raw TSQL here";
        public string Input
        {
            set
            {
                if (input == value) return;
                input = value;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("string query = ");
                bool first = true;
                foreach(string line in input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                {
                    if (string.IsNullOrEmpty(line))
                        continue;
                    if (first)
                        first = false;
                    else
                        sb.AppendLine(" + Environment.NewLine + ");
                    sb.Append("\t\t\t\t\t\t\"" + line + " \"");
                }
                sb.Append(";");
                Output = sb.ToString();
            }
            get { return input; }
        }
        private string output = string.Empty;
        public string Output
        {
            set
            {
                if (output == value) return;
                output = value;
                NotifyPropertyChanged("Output");
            }
            get { return output; }
        }
    }
}

使用系统组件模型;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串信息)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(信息));
}
}
公共主窗口()
{
初始化组件();
}
私有字符串input=“此处输入原始TSQL”;
公共字符串输入
{
设置
{
如果(输入==值)返回;
输入=值;
StringBuilder sb=新的StringBuilder();
sb.AppendLine(“字符串查询=”);
bool first=true;
foreach(input.Split中的字符串行(新字符串[]{Environment.NewLine},StringSplitOptions.None))
{
if(string.IsNullOrEmpty(行))
继续;
如果(第一)
第一个=假;
其他的
sb.AppendLine(“+Environment.NewLine+”);
sb.追加(“\t\t\t\t\t\”+行+“\”);
}
某人加上(“;”);
输出=sb.ToString();
}
获取{返回输入;}
}
私有字符串输出=string.Empty;
公共字符串输出
{
设置
{
如果(输出==值)返回;
产出=价值;
NotifyPropertyChanged(“输出”);
}
获取{返回输出;}
}
}
}


Thorsten的答案适用。如果仍然需要换行符,
“select*\n from hmAdjusted\norder…”
更短。请使用存储的过程,或者如果需要在代码中使用SQL,请将语句存储在设置类中。如果你有超过一小撮的陈述,那么把它们全部写出来(并保持它们)会变得非常枯燥quickly@LeeWillis所以你建议打开内部查询让每个用户修改?@Heinzi我不想要更短的。我想要问题中的格式。@Heinzi我不希望它只产生格式。这就是我希望代码中的格式具有可读性的方式。Thorsten的答案适用。如果仍然需要换行符,
“select*\n from hmAdjusted\norder…”
更短。请使用存储的过程,或者如果需要在代码中使用SQL,请将语句存储在设置类中。如果你有超过一小撮的陈述,那么把它们全部写出来(并保持它们)会变得非常枯燥quickly@LeeWillis所以你建议打开内部查询让每个用户修改?@Heinzi我不想要更短的。我想要问题中的格式。@Heinzi我不希望它只产生格式。这就是我希望代码中的格式具有可读性的方式。也许我的目标是使内容更具可读性。我不是在寻找替代方法。是的,它可以自动化。如果需要的话,我会写一个简短的程序。在我从头开始写之前,也许我想知道是否有一个现有的工具。如果你把它看作是一个是非问题,那么为什么要写关于替代方案的长答案呢?正如第一部分所述,我不是在寻找替代方案。如果你接受书面问题,我特别要求你提供格式。谢谢你的意见。也许我的目标是让东西更人性化。我不是在寻找替代方法。是的,它可以自动化。如果需要的话,我会写一个简短的程序。在我从头开始写之前,也许我想知道是否有一个现有的工具。如果你把它看作是一个是非问题,那么为什么要写关于替代方案的长答案呢?正如第一部分所述,我不是在寻找替代方案。如果你接受书面问题,我特别要求你提供格式。谢谢你的意见。谢谢。这很接近,但会使文本左对齐。为了支持和可读性,我希望它像示例一样缩进。检查我的更新,也许你会发现一些有用的东西,我甚至没有想到正则表达式。我要试一试。手动最后一行不是问题。谢谢。这很接近,但会使文本左对齐。为了支持和可读性,我希望它像示例一样缩进。检查我的更新,也许你会发现一些有用的东西,我甚至没有想到正则表达式。我要试一试。手动最后一行不是问题。它仍然不会自动将原始TSQL转换为C#格式。它仍然不会自动将原始TSQL转换为C#格式。谢谢看到我的答案。我基本上同意这种方法。我特别需要多行输入。我带来的实际问题包括大量的CTE、temp、连接和评论。谢谢你看到我的答案。我基本上同意这种方法。我特别需要多行输入。我带来的实际查询是大量的CTE、temp、JOIN和COMMON
string select = "select * " + Environment.NewLine + 
                "  from hmAdjusted " + Environment.NewLine + 
                " order by [hmAdjusted] desc;";
    public static void Main()
    {
        var query = "select * from hmAdjusted order by [hmAdjusted] desc;";
        var result = MyQueryFormatter(query);
        Console.WriteLine(result);
    }

    public static string MyQueryFormatter(string query)
    {
        var beforeWhatAddNewLine = new string[] { "from", "order" };
        var temp = query.Split(' ');
        var tempLength = temp.Count();
        var result = new StringBuilder();
        for (int i = 0; i < tempLength; i++)
        {
            if (beforeWhatAddNewLine.Contains(temp[i]))
            {
                result.Append(Environment.NewLine);
            }
            else if (i != 0)
            {
                result.Append(" ");
            }
            result.Append(temp[i]);
        }
        return result.ToString();
    }
            var select = string.Join("\n", "select * ",
                                           "  from hmAdjusted ",
                                           " order by [hmAdjusted] desc;");
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        DataContext="{Binding RelativeSource={RelativeSource self}}" 
        Title="SQL Formatter" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TextBox  AcceptsReturn="True" AcceptsTab="True"  
                      Text="{Binding Path=Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </ScrollViewer>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TextBox  Text="{Binding Path=Output, Mode=OneWay}"/>
        </ScrollViewer>
    </Grid>
</Window>

using System.ComponentModel;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
        }
        private string input = "input raw TSQL here";
        public string Input
        {
            set
            {
                if (input == value) return;
                input = value;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("string query = ");
                bool first = true;
                foreach(string line in input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                {
                    if (string.IsNullOrEmpty(line))
                        continue;
                    if (first)
                        first = false;
                    else
                        sb.AppendLine(" + Environment.NewLine + ");
                    sb.Append("\t\t\t\t\t\t\"" + line + " \"");
                }
                sb.Append(";");
                Output = sb.ToString();
            }
            get { return input; }
        }
        private string output = string.Empty;
        public string Output
        {
            set
            {
                if (output == value) return;
                output = value;
                NotifyPropertyChanged("Output");
            }
            get { return output; }
        }
    }
}