简单的控制台学习资料,SQL查询器

来源:互联网 发布:linux下kill tomcat 编辑:程序博客网 时间:2024/06/05 20:52

 

登陆

执行

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.Diagnostics;

 

namespace Sql查询器

{

    class Program

    {

        private static string ServerName = "";

        private static string LoginDataBaseName = "";

        private static string LoginUserName = "";

        private static string LoginPwd = "";

 

        private static SqlConnection Connect = null;

 

        static void Main(string[] args)

        {

            Console.Title = "SQL简单查询器";

            ServerName = Init("服务器地址");

            LoginDataBaseName = Init("服务库名称");

            LoginUserName = Init("登陆用户名");

            LoginPwd = Init("登陆用户口令");

            OpenConnect();            

        }

 

        static void ExecuteSql()

        {

 

            goto Sql;

        Sql:

            {

                Console.Write("SQL:");

                string value = Console.ReadLine();

                if (value == null || value.Trim() == "")

                {

                    goto Sql;

                }

                try

                {

                    Stopwatch watch = new Stopwatch();

                    watch.Start();

                    DateTime BeginTime = DateTime.Now;

                    SqlCommand cmd = new SqlCommand(value, Connect);

                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataTable SqlTable = new DataTable();

                    da.Fill(SqlTable);

                    watch.Stop();

                    Console.WriteLine("总共耗时:" + watch.Elapsed.ToString() + " 记录条数:" + SqlTable.Rows.Count);

 

                    StringBuilder Cols = new StringBuilder();

                    Cols.Append("No ");

                    for (int c = 0; c < SqlTable.Columns.Count; c++)

                    {

                        Cols.Append(SqlTable.Columns[c].ColumnName + " ");                       

                    }                    

                    Console.WriteLine(Cols.ToString().Trim());

 

                    for (int r = 0; r < SqlTable.Rows.Count; r++)

                    {

                        StringBuilder Rows = new StringBuilder();

                        Rows.Append(Convert.ToInt32(r + 1) + " ");

                        for (int c = 0; c < SqlTable.Columns.Count; c++)

                        {

                            if (SqlTable.Rows[r][c] != DBNull.Value)

                            {

                                Rows.Append(SqlTable.Rows[r][c].ToString() + " ");

                            }

                            else

                            {

                                Rows.Append("NULL ");

                            }

                        }

                        Console.WriteLine(Rows.ToString().Trim());

                    }

                    Console.WriteLine("");

                    goto Sql;

                }

                catch (Exception e)

                {

                    Console.WriteLine(e.Message);

                    goto Sql;

                }

            }

        }        

 

        static void OpenConnect()

        {

            if (ServerName == null || ServerName.Trim() == "")

            {

                ServerName = Init("服务器地址");

            }

            if (LoginDataBaseName == null || LoginDataBaseName.Trim() == "")

            {

                LoginDataBaseName = Init("服务器地址");

            }

            string strconnect = "Data Source=" + ServerName + ";Initial Catalog=" + LoginDataBaseName + ";User ID=" + LoginUserName + ";Password=" + LoginPwd + ";Integrated Security=false;Connect Timeout=15;";

            if (Connect == null)

            {

                Connect = new SqlConnection(strconnect);

            }

            if (Connect.State != ConnectionState.Closed)

            {

                try

                {

                    Connect.Close();

                }

                catch { }

            }

            Connect.ConnectionString = strconnect;

            try

            {

                Console.WriteLine("正在连接数据服务器,请候候...");

                Connect.Open();

                Console.Clear();

                ExecuteSql();

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

                if (e.Message.Contains("provider: Named Pipes Provider, error: 40") == true)

                {

                    ServerName = Init("服务器地址");

                    LoginDataBaseName = Init("服务器地址");

                }

                LoginUserName = Init("登陆用户名");

                LoginPwd = Init("登陆用户口令");

                OpenConnect();

            }

        }

 

        static string Init(string Name)

        {

            goto config;

        config:

            {

                Console.Write(Name + ":");

                string value = Console.ReadLine();

                if (value == null || value.Trim() == "")

                {

                    Console.WriteLine(Name + "不能为空,请重新输入!");

                    goto config;

                }

                return value;

            }

        }

 

    }

}

 

原创粉丝点击