⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 利用csharp实现的数据库部署小程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;

using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;
namespace sql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int pos = 0;
        int endPos = 0;
        private const int SB_HORZ = 0x0;
        private const int SB_VERT = 0x1;
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;
        private const int SB_THUMBPOSITION = 4;

        [DllImport("user32.dll")]
        private static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);

        [DllImport("user32.dll")]
        private static extern int GetScrollPos(IntPtr hwnd, int nBar);
        [DllImport("user32.dll")]
        private static extern bool PostMessage(IntPtr hWnd, int nBar, int wParam, int lParam);
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos); 


        //部署数据库
        private void btn_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection();
            if (texServer.Text == "")
            {
                MessageBox.Show("请填写服务器");
            }
               
            else 
             {
                 if (texUser.Text == "")
                     MessageBox.Show("请填写用户名");
                 else if (texPassword.Text == "")
                     MessageBox.Show("请填写密码");
             }
                
            
            sqlConn.ConnectionString = "Data Source= " +texServer .Text + ";"
                + " User id=" + texUser .Text + ";"
                + " Password=" + texPassword.Text + "; Initial Catalog=master";

            try
            {
                sqlConn.Open();
            }
            catch
            {
                MessageBox.Show("Failed to connect to DB!");
                return;
            }
            pgb.Visible = true;
            //Create DB using specific file 
          richTextBox1.Text = "正在进行数据库部署,请稍候..."+"\n";
           bool flag= CreateDB(ref sqlConn);
            //设置控制条
           pgb.Step = 1;
           pgb.PerformStep();
           pgb.Maximum = 100;
            sqlConn.Close();
            sqlConn.Dispose();
           
            if (flag == true)
            {
                richTextBox1.Text = "成功";
                pgb.Value = 100;
                MessageBox.Show("成功");
            }
                
            else 
            MessageBox .Show ("失败");

        }
        private bool CreateDB( ref SqlConnection sqlConn )
        {
            string strQuery;
            bool flag = false;
            if (ReadSQLFromFile(out strQuery))
            {
                strQuery = strQuery.Replace("\r\n", " ");
                strQuery = strQuery.Replace(" GO ", " ; ");

                SqlCommand sqlComm = new SqlCommand(strQuery, sqlConn);

                try
                {
                    sqlComm.ExecuteNonQuery();
                    flag = true;

                }
                catch (SqlException sqlErr)
                {
                    MessageBox.Show(sqlErr.Message);
                    flag = false;

                }
                catch
                {
                }

                sqlComm.Dispose();
            }
            else
                flag = false;
            return flag ;
        }
        private bool ReadSQLFromFile(out string strQuery)
        {
            string strFileName = Application.StartupPath + @"\SQLQuery9.sql";//Give specific SQL file 
            //string strPath = ;
          //  strFileName = strPath + "\\" + strFileName;
            strQuery = ""; //Init return value

            if (File.Exists(strFileName))
            {
                StreamReader sr = File.OpenText(strFileName);
                strQuery = sr.ReadToEnd();

                sr.Close();
                return true;
            }
            else
                return false;
        }

        private void texUser_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pgb.Visible = false;
        }

        private void pgb_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
             pos = GetScrollPos(richTextBox1.Handle, SB_VERT);//加上这句是为了如果用户手动拖拽滚动条,可以保证滚动条继续从拖拽的位置走 
             pos++; 
            //如果已经到底,那么停止Timer 
            if (pos > endPos) 
            { 
                this.timer1.Enabled = false; 
                return; 
            } 
            SetScrollPos(richTextBox1.Handle, SB_VERT, pos, true); 
            PostMessage(richTextBox1.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * pos, 0); 
        //本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/winform/20081217/3141.html

        }

    }

    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -