How to execute an .SQL script file using c#

后端 未结 10 895
忘了有多久
忘了有多久 2020-11-22 17:03

I\'m sure this question has been answered already, however I was unable to find an answer using the search tool.

Using c# I\'d like to run a .sql file. The sql file

10条回答
  •  遥遥无期
    2020-11-22 17:37

    I couldn't find any exact and valid way to do this. So after a whole day, I came with this mixed code achieved from different sources and trying to get the job done.

    But it is still generating an exception ExecuteNonQuery: CommandText property has not been Initialized even though it successfully runs the script file - in my case, it successfully creates the database and inserts data on the first startup.

    public partial class Form1 : MetroForm
    {
        SqlConnection cn;
        SqlCommand cm;
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!CheckDatabaseExist())
            {
                GenerateDatabase();
            }
        }
    
        private bool CheckDatabaseExist()
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=SalmanTradersDB;Integrated Security=true");
            try
            {
                con.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }
    
        private void GenerateDatabase()
        {
    
            try
            {
                cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
                StringBuilder sb = new StringBuilder();
                sb.Append(string.Format("drop databse {0}", "SalmanTradersDB"));
                cm = new SqlCommand(sb.ToString() , cn);
                cn.Open();
                cm.ExecuteNonQuery();
                cn.Close();
            }
            catch
            {
    
            }
            try
            {
                //Application.StartupPath is the location where the application is Installed
                //Here File Path Can Be Provided Via OpenFileDialog
                if (File.Exists(Application.StartupPath + "\\script.sql"))
                {
                    string script = null;
                    script = File.ReadAllText(Application.StartupPath + "\\script.sql");
                    string[] ScriptSplitter = script.Split(new string[] { "GO" }, StringSplitOptions.None);
                    using (cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"))
                    {
                        cn.Open();
                        foreach (string str in ScriptSplitter)
                        {
                            using (cm = cn.CreateCommand())
                            {
                                cm.CommandText = str;
                                cm.ExecuteNonQuery();
                            }
                        }
                    }
                }
            }
            catch
            {
    
            }
    
        }
    
    }
    

提交回复
热议问题