编辑: 没心没肺DR | 2019-07-06 |
怎样制作一个简单的winform安装程序? ① 建一个WinForm应用程序,最最简单的那种.运行. ② 添加新项目->安装和部署项目,'模板'选择'安装向导'. ③ 连续二个'下一步',在'选择包括的项目输出'步骤打勾'主输出来自',连续两个'下一步','完成'. ④ 生成. ⑤ 到项目目录下找到Setup.exe(还有一个.msi和.ini文件),执行. 31.怎样通过winform安装程序在Sql Server数据库上建表? ① [项目]―[添加新项] 类别:代码;
模板:安装程序类. 名称:MyInstaller.cs ② 在SQL Server建立一个表,再[所有任务]―[生成SQL脚本]. 生成类似如下脚本(注意:把所有GO语句去掉): if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[MyTable] CREATE TABLE [dbo].[MyTable] ( [ID] [int] NOT NULL , [NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL ) ON [PRIMARY] ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] ③ [项目]―[添加现有项].mytable.sql―[生成操作]-[嵌入的资源]. ④ 将MyInstaller.cs切换到代码视图,添加下列代码: 先增加: using System.Reflection;
using System.IO;
然后: private string GetSql(string Name) { try { Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
} catch (Exception ex) { Console.Write("In GetSql:"+ex.Message);
throw ex;
} } private void ExecuteSql(string DataBaseName,string Sql) { System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
sqlConn.ConnectionString = "server=myserver;
uid=sa;
password=;
database=master";
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try { Command.ExecuteNonQuery();
} finally { Command.Connection.Close();
} } protected void AddDBTable(string strDBName) { try { ExecuteSql("master","create DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("mytable.sql"));
} catch(Exception ex) { Console.Write("In exception handler :"+ex.Message);
} } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver);
AddDBTable("MyDB");
//建一个名为MyDB的DataBase } ⑤ [添加新项目]―[项目类型:安装和部署项目]―[模板:安装项目]―[名称:MySetup]. ⑥ [应用程序文件夹]―[添加]―[项目输出]―[主输出]. ⑦ 解决方案资源管理器―右键―[安装项目(MySetup)]―[视图]―[自定义操作].[安装]―[添加自定义操作]―[双击:应用程序文件夹]的[主输出来自***(活动)]. 32.怎样用TreeView显示父子关系的数据库表(winform)? 三个表a1,a2,a3, a1为a2看母表,a2为a3的母表. a1: id, name a2: id, parent_id, name a3: id, parent_id, name 用三个DataAdapter把三个表各自Fill进DataSet的三个表. 用DataRelation设置好三个表之间的关系. foreach(DataRow drA1 in ds.Tables["a1"].Rows) { tn1 = new TreeNode(drA1["name"].ToString());
treeView1.Nodes.Add(tn1);
foreach(DataRow drA2 in drA1.GetChildRows("a1a2")) { tn2 = new TreeNode(drA2["name"].ToString());
tn1.Nodes.Add(tn2);
foreach(DataRow drA3 in drA2.GetChildRows("a2a3")) { tn3 = new TreeNode(drA3["name"].ToString());