现在的位置: 首页 > 综合 > 正文

Some Sql Server deploy scenarios

2012年06月19日 ⁄ 综合 ⁄ 共 984字 ⁄ 字号 评论关闭
I got a good MSDN help: Embedding SQL Server Express into Custom Applications
It introduces methods to deploy by a class wrapper. (Usually, we create a setup project and add SQL Express as prerequisite to deploy)
And it also gives an idea to embed *.sql script as text resource and then read, execute the commands one by one:
private SqlConnection sqlCon = new SqlConnection();
private SqlCommand sqlCmd = new SqlCommand();
public string[] ParseScriptToCommands(string strScript)
{
string[] commands;
commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);
return commands;
}

public bool RunScript(string strFile)
{
string[] strCommands;
strCommands = ParseScriptToCommands(strFile);
try
{
if (sqlCon.State != ConnectionState.Open) sqlCon.Open();

sqlCmd.Connection = sqlCon;

foreach (string strCmd in strCommands)
{
if (strCmd.Length > 0)
{
sqlCmd.CommandText = strCmd;
sqlCmd.ExecuteNonQuery();
}
}
}
catch (SqlException sql_ex)
{
MessageBox.Show(sql_ex.Number.ToString() + " " +
sql_ex.Message.ToString());
return false;
}

return true;
}
Additional resource: Deploy SQL Server databases easily with an Installer class

抱歉!评论已关闭.