Cách tạo setup project visual studio với sql

45 338 0
Cách tạo setup project visual studio với sql

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Project QLSVApplication: là ứng dụng dùng để hiển thị thông tin sinh viên, chỉ là 1 Datagridview để hiển thị lên.Project SetupEngine : là Project loại Libraries, project này chúng ta sẽ thêm loại Installer đặt tên là InstallerEngine (Class này có nhiệm vụ cài đặt Cơ sở dữ liệu vào máy tính, với các thông số được truyền vào từ quá trình cài đặt do ta quy định). Ta sẽ nhúng các SQL Script mà SQLServer cung cấp cho ta vào đây với tên sqldata.txt và sqldropcreate.txt (nhớ đặt tên viết thường, ta sẽ đi vào chi tiết ở phần sau)Ta sẽ biên dịch project này thành dll để sử dụng trong Project QLSVSetup.Project QLSVSetup : Dùng để cài đặt ứng dụng QLSVApplication vào máy tính, và ra lệnh cho SetupEngine cài đặt cơ sở dữ liệu

Cách tạo setup project visual studio với Sql server Tạo Solution có chứa Project bên dưới: Project QLSVApplication: ứng dụng dùng để hiển thị thông tin sinh viên, Datagridview để hiển thị lên Project SetupEngine : Project loại Libraries, project thêm loại Installer đặt tên InstallerEngine (Class có nhiệm vụ cài đặt Cơ sở liệu vào máy tính, với thông số truyền vào từ trình cài đặt ta quy định) Ta nhúng SQL Script mà SQLServer cung cấp cho ta vào với tên sqldata.txt sqldropcreate.txt (nhớ đặt tên viết thường, ta vào chi tiết phần sau) Ta biên dịch project thành dll để sử dụng Project QLSVSetup Project QLSVSetup : Dùng để cài đặt ứng dụng QLSVApplication vào máy tính, lệnh cho SetupEngine cài đặt sở liệu Bước 1: Tạo ứng dụng sử dụng Project QLSVApplication: Trong project đơn giản hiển thị thông tin, chuỗi kết nối đọc từ app.config Chuỗi cấp nhập trình cài đặt vào máy tính Bước 2: Tạo Project để cài đặt CSDL Project SetupEngine: Project dùng để tạo CSDL, Project nhớ chọn loại Libraries – Để tạo class Installer project: Bấm chuột phải vào Project/ chọn Add / New Item Trong sổ lên tìm tới loại Installer Class, đặt tên class InstallerEngine – Đặt tên class InstallerEngine nhấn Add – Tiếp tục tạo Text File để lưu SQL Script (các Script lấy công cụ SQL Server cho lẹ) Ở ta chia làm tập tin SQL Script lý sau: Thao tác tạo CSDL cần phải có thời gian chờ để kịp update vào SQL Server Ta cho chờ thời gian sau tiếp tục chạy SQL Script tạo bảng, insert liệu… – Để tạo Text File: Bấm chuột phải vào Project / Add/ New Item / chọn Text File nhập tên – Trên tạo Text File tên sqldropcreate.txt , ý nhớ đặt tên viết thường toàn – Sau tạo xong tập tin sqldropcreate.txt, chép đoạn Script tạo CSDL vào (xem hình ): –>Các script có sẵn SQL Server tự tạo nên ta cần copy từ vào (nhớ bỏ hết dòng có chữ Go) – Tiếp theo ta phải cấu hình để Text File nhúng vào Resource sau biên dịch – Trong Properties: Lần lượt chọn Text File chọn Embedded Resource Build Action – Bây tiến hành chỉnh sủa class InstallerEngine: using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Configuration.Install; using System.Linq; using System.IO; using System.Reflection; using System.Data.SqlClient; using System.Xml; using System.Collections.Specialized; namespace SetupEngine { [RunInstaller(true)] public partial class InstallerEngine : System.Configuration.Install.Installer { private string logFilePath =””; private string pathApp = “”; public InstallerEngine() { InitializeComponent(); } private string GetSql(string Name) { try { // Gets the current assembly Assembly Asm = Assembly.GetExecutingAssembly(); // Resources are named using a fully qualified name Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + “.” + Name); // Reads the contents of the embedded file StreamReader reader = new StreamReader(strm); string sInfor = reader.ReadToEnd(); Log(sInfor); reader.Close(); return sInfor; } catch (Exception ex) { Log(ex.ToString()); throw ex; } } private void ExecuteSql(string serverName, string dbName, string userid, string password, string Sql) { string connStr = “server =” + serverName + “;database =” + dbName + “;uid=” + userid + “;pwd=” + password; using (SqlConnection conn = new SqlConnection(connStr)) { try { SqlCommand cmd = new SqlCommand(Sql); conn.Open(); cmd.Connection = conn; int n = cmd.ExecuteNonQuery(); Log(” n= ” + n); conn.Close(); } catch (Exception ex) { Log(ex.ToString()); } } } protected void AddDBTable(string serverName, string userid, string password) { try { // Creates the database and installs the tables string strScript = GetSql(“sqldropcreate.txt”); ExecuteSql(serverName, “master”, userid, password, strScript); System.Threading.Thread.Sleep(60 * 1000); strScript = GetSql(“sqldata.txt”); ExecuteSql(serverName, “dbqlsv”, userid, password, strScript); System.Threading.Thread.Sleep(60 * 1000); string connStr = “server =” + serverName + “;database =dbqlsv;uid=” + userid + “;pwd=” + password; Log(“AppPath=” + pathApp); XmlDocument xmlDom = new XmlDocument(); xmlDom.Load(pathApp); // Get XML node XmlNode xmlNode = xmlDom.SelectSingleNode( “configuration/appSettings/add[@key=’MYCONN’]”); xmlNode.Attributes[“value”].Value = connStr; // Updating connection string in file Log(“Followind node of config file will be updated: ” + xmlNode.InnerXml); // Save to disk xmlDom.Save(pathApp); } catch (Exception ex) { //Reports any errors and abort Log(ex.ToString()); throw ex; } } protected override void OnAfterInstall(IDictionary savedState) { base.OnAfterInstall(savedState); } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); string assemPath = this.Context.Parameters[“assemblypath”]; int pos = assemPath.LastIndexOf(“\”); logFilePath = assemPath Substring(0,pos+1)+ “\SetupLog117.txt”; pathApp = assemPath.Substring(0, pos + 1) + “\QLSVApplication.exe.config”; Log(“—-Setup started—-“); Log(“Server=” + this.Context.Parameters[“servername”] + ” ; User Id=” + this.Context.Parameters[“userid”] + ” ; pwd=” + this.Context.Parameters[“password”]); foreach (DictionaryEntry s in this.Context.Parameters) { Log(“Parameter : “+s.Key +” ; value =”+s.Value); } AddDBTable(this.Context.Parameters[“servername”], this.Context.Parameters[“userid”], this.Context.Parameters[“password”]); } public void Log(string str) { StreamWriter Tex; try { 10 conn.Close(); } catch (Exception ex) { Log(ex.ToString()); } } } protected void AddDBTable(string serverName, string userid, string password) { try { // Creates the database and installs the tables string strScript = GetSql(“sqldropcreate.txt”); ExecuteSql(serverName, “master”, userid, password, strScript); System.Threading.Thread.Sleep(60 * 1000); strScript = GetSql(“sqldata.txt”); ExecuteSql(serverName, “dbqlsv”, userid, password, strScript); System.Threading.Thread.Sleep(60 * 1000); string connStr = “server =” + serverName + “;database =dbqlsv;uid=” + userid + “;pwd=” + password; Log(“AppPath=” + pathApp); 31 XmlDocument xmlDom = new XmlDocument(); xmlDom.Load(pathApp); // Get XML node XmlNode xmlNode = xmlDom.SelectSingleNode( “configuration/appSettings/add[@key=’MYCONN’]”); xmlNode.Attributes[“value”].Value = connStr; // Updating connection string in file Log(“Followind node of config file will be updated: ” + xmlNode.InnerXml); // Save to disk xmlDom.Save(pathApp); } catch (Exception ex) { //Reports any errors and abort Log(ex.ToString()); throw ex; } } protected override void OnAfterInstall(IDictionary savedState) { base.OnAfterInstall(savedState); 32 } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); string assemPath = this.Context.Parameters[“assemblypath”]; int pos = assemPath.LastIndexOf(“\\”); logFilePath = assemPath Substring(0,pos+1)+ “\\SetupLog117.txt”; pathApp = assemPath.Substring(0, pos + 1) + “\\QLSVApplication.exe.config”; Log(“—-Setup started—-“); Log(“Server=” + this.Context.Parameters[“servername”] + ” ; User Id=” + this.Context.Parameters[“userid”] + ” ; pwd=” + this.Context.Parameters[“password”]); foreach (DictionaryEntry s in this.Context.Parameters) { Log(“Parameter : “+s.Key +” ; value =”+s.Value); } AddDBTable(this.Context.Parameters[“servername”], this.Context.Parameters[“userid”], this.Context.Parameters[“password”]); } public void Log(string str) { StreamWriter Tex; try 33 { Tex = File.AppendText(this.logFilePath); Tex.WriteLine(DateTime.Now.ToString() + ” ” + str); Tex.Close(); } catch {} } } } Giải thích số dòng lệnh bên trên: Hàm GetSql(string Name) : Dùng để đọc Text File Sql Script Vì tập tin nhúng vào Assembly nên chế đọc tập tin (xem code) Lệnh : this.Context.Parameters[“assemblypath”]; lấy đường dẫn mà lúc cài đặt chương trình người sử dụng chọn Key assemblypath có sẵn, phải viết y chang this.Context.Parameters[“servername”], this.Context.Parameters[“userid”], this.Context.Parameters[“password”] servernam, userid, password quy định, đặt bên Project Setup, biến phải đặt y chang biến mà bên Project Setup ta đặt void AddDBTable(string serverName, string userid, string password) có nhiệm vụ xóa tạo CSDL sau tạo bảng, liệu sau tạo xong tự động cập nhập file App.config cho chương trình (dùng xml) 34 Bước 3: Tạo Project Setup Chọn Setup project: đặt tên QLSVSetup bấm OK Bấm chuột phải vào QLSVSetup/ chọn File System: 35 Tại cửa sổ ta bấm chuột vào thư mục Application Folder, để có thông tin bên phải hình ta làm sau: Bấm chuột phải vào Application Folder / Add/ Project Output… Tiếp tục thêm tập tin ứng dụng app.config bên Project QLSVApplication vào đây: 36 Bấm chuột phải vào Application Folder / Add/ File: Kết quả: Để tạo Shortcut cho ứng dụng sau cài đặt ta bấm chuột phải vào QLSVApplication.exe 37 Sau cắt vào User’s Desktop hay User’s programs Menu, ta tạo thư mục bên nhánh trái, chép Shortcut vào đó, tạo nhiều shortcut Tiếp theo ta cấu hình giao diện cài đặt, bấm chuột vào QLSVSetup, bên ta chọn Icon User Interface Editor: Màn hình User interface xuất hiện: 38 Cửa sổ Add Dialog hiển thị lên: 39 Cấu hình TextBoxes (A) hình chụp bên dưới: Các tên : CUSTOMTEXTA1, CUSTOMTEXTA2, CUSTOMTEXTA3 ta đặt để bên Custom Action tham chiếu lấy giá trị từ hình cài đặt Không dùng Edit4Property nên to cho Edit4Visible =false – Tiếp theo, cấu hình Custom Action: Bấm chuột phải vào QLSVSetup/ chọn View/ chọn Custom Actions 40 Tại cửa sổ Custom Actions, Bấm chuột phải vào Install / chọn Add Custom Action… Chọn Primary ouput from SetupEngine (active) bấm OK 41 Sau click chuột vào Primary output, quan sát Properties, Ta cấu hình CustomActionData hình: /servername=[CUSTOMTEXTA1] /userid=[CUSTOMTEXTA2] /password=[CUSTOMTEXTA3] Ta viết y chang trên, ý biến servername, userid, password ta đặt bên sử dụng cho bên SetupEngine : this.Context.Parameters[“servername”], this.Context.Parameters[“userid”], this.Context.Parameters[“password”] Tức ta đặt tên bên SetupEngine phải lấy tên ta đặt bên CUSTOMTEXTA1 , CUSTOMTEXTA2, CUSTOMTEXTA3 ta đặt cho EditPropertie Textboxes (A) 42 Bước 4: biên dịch cài đặt – Cấu hình QLSVSetup bên Chọn Rebuild để biên dịch Setup Sau chọn Install để cài đặt: Các bước cài đặt: 43 BẤM next, tự động xuất hình cấu hình CSDL (chính Textboxes (A)) Nhập thông tin bấm Next: Chọn đường dẫn cài đặt bấm Next … xong Chương trình cài đặt ứng dụng đồng thời cài đặt SQL (có cập nhật kết nối cho ta luôn) Kết quan sát hình Desktop khởi động chương trình: 44 45 ... Print • Posted in: C#2 22 Cách tạo setup project visual studio với Sql server By Trần Duy Thanh on November 9, 2012 | Comments Votes Tạo Solution có chứa Project bên dưới: Project QLSVApplication:...Bước 2: Tạo Project để cài đặt CSDL Project SetupEngine: Project dùng để tạo CSDL, Project nhớ chọn loại Libraries – Để tạo class Installer project: Bấm chuột phải vào Project/ chọn Add... tính 24 Bước 2: Tạo Project để cài đặt CSDL Project SetupEngine: Project dùng để tạo CSDL, Project nhớ chọn loại Libraries – Để tạo class Installer project: Bấm chuột phải vào Project/ chọn Add

Ngày đăng: 14/08/2017, 20:31

Mục lục

  • Cách tạo setup project visual studio với Sql server

    • Chia sẻ lên:

    • Cách tạo setup project visual studio với Sql server

Tài liệu cùng người dùng

Tài liệu liên quan