最新评论

共3页: 1 2 3 下一页 
路过,欢迎到我的RoseGril.cn交流下,如有打扰请见谅。谢谢! Create At:18:56:55
我今天刚在做,同样的方法。
我安装程序后,数据库可以生成,但是数据库中的存储过程和表没有生成。
这是哪里出错了?
晕,难道用SQLDmo不好吗?

public bool ExecSQLFile()
{
SQLDMO.SQLServer2 sqlserver=new SQLDMO.SQLServer2Class();
try
{
sqlserver.Connect(Server,Uid,Pwd);
SQLDMO.Databases mydbs=sqlserver.Databases;
SQLDMO.Database mydb =new SQLDMO.DatabaseClass ();
mydb=(SQLDMO.Database)mydbs.Item(_Database,Uid);
System.IO.StreamReader s=new System.IO.StreamReader(_SQLFilePath);
string sql=s.ReadToEnd();
mydb.ExecuteImmediate(sql,SQLDMO.SQLDMO_EXEC_TYPE.SQLDMOExec_Default,sql.Length );
return true;
}
catch(Exception e)
{
throw e;
}
finally
{
sqlserver.DisConnect();
}
}


引用Interop.SQLDMO.dll,自动打包,还可以轻松实现库备份与恢复.

public bool ExecBackup()
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(_Server,_Uid, _Pwd);
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = _Database;
oBackup.Files = @_BackupPath;
oBackup.BackupSetName = _Database;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
return true;
}
catch(Exception ex)
{
throw ex;
}
finally
{
oSQLServer.DisConnect();
}
}

public bool ExecRestore()
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
exepro();
oSQLServer.LoginSecure = false;
oSQLServer.Connect(_Server,_Uid, _Pwd);
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = _Database;
/**////自行修改
oRestore.Files = @_RestorePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);

return true;
}
catch(Exception e)
{
throw e;
}
finally
{
oSQLServer.DisConnect();
}
}

我这里有一个完整的类,有兴趣的朋友加我QQ啊58730381

还有一个方法,针对SQL SERVER 2005的可以用:

#region 适用于SQL Server 2005
// //调用SQLCMD执行脚本
// System.Diagnostics.Process SqlProcess = new System.Diagnostics.Process();
// SqlProcess.StartInfo.FileName = "SQLCMD.exe ";
//
//
//
// SqlProcess.StartInfo.Arguments = string.Format(" -S {0} -U {1} -P {2} -d {3} -i {4}SWSchool5.sql", this.Context.Parameters["server"],this.Context.Parameters["user"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"], this.Context.Parameters["targetdir"]+"Databases\\");
// SqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// SqlProcess.Start();
// SqlProcess.WaitForExit(); //等待执行
// SqlProcess.Close();
#endregion
re: 用ViewState保存对象! lili 2006-09-25 19:03  
希望把十一月四日风雨大作<二>的诗意发上去!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
gljhgl 各个 2006-04-04 18:23  
xccvb
re: 用ViewState保存对象! 各个 2006-04-04 18:19  
希望把十一月四日风雨大作<二>的诗意发上去。

呵呵~~~
re: 用ViewState保存对象! 赌东道 2006-04-04 18:17  
希望把十一月四日风雨大作<二>的诗意发上去
re: 用ViewState保存对象! 闂堟瑤闄?Url=閺? 6828EntryAggViewIsOkay=HasViewed 2006-04-04 18:13  
对啊!!!!!!!!!!!1
@静丽
re: 用ViewState保存对象! 静丽 2006-04-04 18:12  
怎么会没有十一月四日风雨大作《二》的诗意呀??????????
re: 用ViewState保存对象! 闈欎附 2006-04-04 18:12  
十一月四日风雨大作<二>陆游
你好我是一个初学者在数据库部署的这个问题上我还有一个不明白的地方.就是怎么才能把数据库的脚本文件DB.SQL加上去啊这个文件是在什么时候生成的呀.
谢谢初学不好意思?
有空给我发一份
zhizhen@sohu.com
谢了!!
为什么我安装完之后在SqlServer服务器上找不到安装的数据库呢?
安装过程中没有提示任何错误 ,我使用的是用户名和密码连接的数据库
为什么我安装完之后在SqlServer服务器上找不到安装的数据库呢?
安装过程中没有提示任何错误 ,我使用的是用户名和密码连接的数据库
晕还限制内容长度啊,再贴
-----------------------------------------------------------
// ---------------------将连接字符串写入Web.config-----------------------------------

try
{
FileInfo fileInfo =new FileInfo(this.Context.Parameters["targetdir"] + "\\Web.config");
if(!fileInfo.Exists)
throw new InstallException("没有找到配置文件!");
//实例化XML文档
XmlDocument doc=new XmlDocument();
doc.Load(fileInfo.FullName);
//查找到appSettings中的节点
bool foundIt=false;
foreach(XmlNode node in doc["configuration"]["appSettings"])
{
if(node.Name=="add")
{
if(node.Attributes.GetNamedItem("key").Value=="connString")
{
//写入连接字符串
node.Attributes.GetNamedItem("value").Value=String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1",
this.Context.Parameters["server"],this.Context.Parameters["dbname"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
foundIt=true;
}
}
}

if(!foundIt)
{
throw new InstallException("web.Config 文件没有包含connString连接字符串设置");
}
doc.Save(fileInfo.FullName);
}
catch(Exception ex)
{
throw ex;
}

}


#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
to wngwz@msn.com :
我给你发过了,但被退了回来,我找了好久才找到源码,贴到这里吧
--------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Diagnostics;

namespace DbCustomAction
{
/// <summary>
/// DbCustomAction 的摘要说明。
/// </summary>
[RunInstaller(true)]
public class DbCustomAction : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public DbCustomAction()
{
// 该调用是设计器所必需的。
InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


// 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 connString,string dbname,string sqlString)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd=new SqlCommand(sqlString,conn);
cmd.Connection.Open();
cmd.Connection.ChangeDatabase(dbname);
try
{
cmd.ExecuteNonQuery();
}
finally
{
cmd.Connection.Close();
}
}


public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);

// ------------------------建立数据库-------------------------------------------------
try
{
string connString=String.Format("server={0};uid={1};pwd={2}",
this.Context.Parameters["server"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
//根据输入的数据库名称建立数据库
ExecuteSql(connString,"master","CREATE DATABASE "+ this.Context.Parameters["dbname"]);

// //用ado.net执行脚本 ,但这段代码有问题,因为数据库服务器上没有sql.txt文件
// string sqlString = String.Format("EXEC MASTER..XP_CMDSHELL 'OSQL -U {0} -P {1} -d {2} -b -i {3}'",
// this.Context.Parameters["user"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"], "sql.txt");
// //this.Context.Parameters["user"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"], this.Context.Parameters["targetdir"]);
// ExecuteSql(connString, this.Context.Parameters["dbname"] , sqlString);


//如果安装web项目的机器上没有安装mssql,就无法使用osql
// 象这样web应用与数据库不放在同一台服务器上的情况,可以用ado.net执行osql
//调用osql执行脚本,现在的解决方案是把osql.exe打包到安装包中
Process process=new Process();
process.StartInfo.FileName = this.Context.Parameters["targetdir"]+"osql.exe";
process.StartInfo.Arguments=String.Format(" -U {0} -P {1} -S {2} -d {3} -i {4}sql.txt",
this.Context.Parameters["user"],this.Context.Parameters["pwd"], this.Context.Parameters["server"],this.Context.Parameters["dbname"], this.Context.Parameters["targetdir"]);
process.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit(); //等待执行
process.Close();


//删除脚本文件
FileInfo fileInfo=new FileInfo(String.Format("{0}sql.txt", this.Context.Parameters["targetdir"]));
if(fileInfo.Exists)
fileInfo.Delete();

//删除osql.exe
fileInfo=new FileInfo(String.Format("{0}osql.exe", this.Context.Parameters["targetdir"]));
if(fileInfo.Exists)
fileInfo.Delete();


}
catch(Exception ex)
{
throw ex;
}

lifeng@winspace.net
谢谢兄台!
谢谢!发给我一分吧!
wngwz@msn.com
谢谢floodzhu,你说得没错,我以前也挺过,但是没注意字符集的问题:)
共3页: 1 2 3 下一页